Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
576 serge 1
/*
2
	layer2.c: the layer 2 decoder, root of mpg123
3
 
4
	copyright 1994-2006 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.de
6
	initially written by Michael Hipp
7
 
8
	mpg123 started as mp2 decoder a long time ago...
9
*/
10
 
11
#include "mpg123.h"
12
#include "l2tables.h"
13
 
14
static int grp_3tab[32 * 3] = { 0, };   /* used: 27 */
15
static int grp_5tab[128 * 3] = { 0, };  /* used: 125 */
16
static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
17
 
18
real muls[27][64];	/* also used by layer 1 */
19
 
20
void init_layer2(void)
21
{
22
  static double mulmul[27] = {
23
    0.0 , -2.0/3.0 , 2.0/3.0 ,
24
    2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
25
    2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
26
    2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
27
    -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
28
    -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0 };
29
  static int base[3][9] = {
30
     { 1 , 0, 2 , } ,
31
     { 17, 18, 0 , 19, 20 , } ,
32
     { 21, 1, 22, 23, 0, 24, 25, 2, 26 } };
33
  int i,j,k,l,len;
34
  real *table;
35
  static int tablen[3] = { 3 , 5 , 9 };
36
  static int *itable,*tables[3] = { grp_3tab , grp_5tab , grp_9tab };
37
 
38
  for(i=0;i<3;i++)
39
  {
40
    itable = tables[i];
41
    len = tablen[i];
42
    for(j=0;j
43
      for(k=0;k
44
        for(l=0;l
45
        {
46
          *itable++ = base[i][l];
47
          *itable++ = base[i][k];
48
          *itable++ = base[i][j];
49
        }
50
  }
51
 
52
  for(k=0;k<27;k++)
53
  {
54
    double m=mulmul[k];
55
    table = muls[k];
56
#ifdef USE_MMX
57
    if(!param.down_sample)
58
        for(j=3,i=0;i<63;i++,j--)
59
    *table++ = 16384 * m * pow(2.0,(double) j / 3.0);
60
    else
61
#endif
62
    for(j=3,i=0;i<63;i++,j--)
63
      *table++ = m * pow_test(2.0,(double) j / 3.0);
64
    *table++ = 0.0;
65
  }
66
}
67
 
68
 
69
void II_step_one(unsigned int *bit_alloc,int *scale,struct frame *fr)
70
{
71
    int stereo = fr->stereo-1;
72
    int sblimit = fr->II_sblimit;
73
    int jsbound = fr->jsbound;
74
    int sblimit2 = fr->II_sblimit<
75
    struct al_table *alloc1 = fr->alloc;
76
    int i;
77
    static unsigned int scfsi_buf[64];
78
    unsigned int *scfsi,*bita;
79
    int sc,step;
80
 
81
    bita = bit_alloc;
82
    if(stereo)
83
    {
84
      for (i=jsbound;i;i--,alloc1+=(1<
85
      {
86
        *bita++ = (char) getbits(step=alloc1->bits);
87
        *bita++ = (char) getbits(step);
88
      }
89
      for (i=sblimit-jsbound;i;i--,alloc1+=(1<
90
      {
91
        bita[0] = (char) getbits(step=alloc1->bits);
92
        bita[1] = bita[0];
93
        bita+=2;
94
      }
95
      bita = bit_alloc;
96
      scfsi=scfsi_buf;
97
      for (i=sblimit2;i;i--)
98
        if (*bita++)
99
          *scfsi++ = (char) getbits_fast(2);
100
    }
101
    else /* mono */
102
    {
103
      for (i=sblimit;i;i--,alloc1+=(1<
104
        *bita++ = (char) getbits(step=alloc1->bits);
105
      bita = bit_alloc;
106
      scfsi=scfsi_buf;
107
      for (i=sblimit;i;i--)
108
        if (*bita++)
109
          *scfsi++ = (char) getbits_fast(2);
110
    }
111
 
112
    bita = bit_alloc;
113
    scfsi=scfsi_buf;
114
    for (i=sblimit2;i;i--)
115
      if (*bita++)
116
        switch (*scfsi++)
117
        {
118
          case 0:
119
                *scale++ = getbits_fast(6);
120
                *scale++ = getbits_fast(6);
121
                *scale++ = getbits_fast(6);
122
                break;
123
          case 1 :
124
                *scale++ = sc = getbits_fast(6);
125
                *scale++ = sc;
126
                *scale++ = getbits_fast(6);
127
                break;
128
          case 2:
129
                *scale++ = sc = getbits_fast(6);
130
                *scale++ = sc;
131
                *scale++ = sc;
132
                break;
133
          default:              /* case 3 */
134
                *scale++ = getbits_fast(6);
135
                *scale++ = sc = getbits_fast(6);
136
                *scale++ = sc;
137
                break;
138
        }
139
 
140
}
141
 
142
void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,struct frame *fr,int x1)
143
{
144
    int i,j,k,ba;
145
    int stereo = fr->stereo;
146
    int sblimit = fr->II_sblimit;
147
    int jsbound = fr->jsbound;
148
    struct al_table *alloc2,*alloc1 = fr->alloc;
149
    unsigned int *bita=bit_alloc;
150
    int d1,step;
151
 
152
    for (i=0;i
153
    {
154
      step = alloc1->bits;
155
      for (j=0;j
156
      {
157
        if ( (ba=*bita++) )
158
        {
159
          k=(alloc2 = alloc1+ba)->bits;
160
          if( (d1=alloc2->d) < 0)
161
          {
162
            real cm=muls[k][scale[x1]];
163
            fraction[j][0][i] = ((real) ((int)getbits(k) + d1)) * cm;
164
            fraction[j][1][i] = ((real) ((int)getbits(k) + d1)) * cm;
165
            fraction[j][2][i] = ((real) ((int)getbits(k) + d1)) * cm;
166
          }
167
          else
168
          {
169
            static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
170
            unsigned int idx,*tab,m=scale[x1];
171
            idx = (unsigned int) getbits(k);
172
            tab = (unsigned int *) (table[d1] + idx + idx + idx);
173
            fraction[j][0][i] = muls[*tab++][m];
174
            fraction[j][1][i] = muls[*tab++][m];
175
            fraction[j][2][i] = muls[*tab][m];
176
          }
177
          scale+=3;
178
        }
179
        else
180
          fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
181
      }
182
    }
183
 
184
    for (i=jsbound;i
185
    {
186
      step = alloc1->bits;
187
      bita++;	/* channel 1 and channel 2 bitalloc are the same */
188
      if ( (ba=*bita++) )
189
      {
190
        k=(alloc2 = alloc1+ba)->bits;
191
        if( (d1=alloc2->d) < 0)
192
        {
193
          real cm;
194
          cm=muls[k][scale[x1+3]];
195
          fraction[1][0][i] = (fraction[0][0][i] = (real) ((int)getbits(k) + d1) ) * cm;
196
          fraction[1][1][i] = (fraction[0][1][i] = (real) ((int)getbits(k) + d1) ) * cm;
197
          fraction[1][2][i] = (fraction[0][2][i] = (real) ((int)getbits(k) + d1) ) * cm;
198
          cm=muls[k][scale[x1]];
199
          fraction[0][0][i] *= cm; fraction[0][1][i] *= cm; fraction[0][2][i] *= cm;
200
        }
201
        else
202
        {
203
          static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
204
          unsigned int idx,*tab,m1,m2;
205
          m1 = scale[x1]; m2 = scale[x1+3];
206
          idx = (unsigned int) getbits(k);
207
          tab = (unsigned int *) (table[d1] + idx + idx + idx);
208
          fraction[0][0][i] = muls[*tab][m1]; fraction[1][0][i] = muls[*tab++][m2];
209
          fraction[0][1][i] = muls[*tab][m1]; fraction[1][1][i] = muls[*tab++][m2];
210
          fraction[0][2][i] = muls[*tab][m1]; fraction[1][2][i] = muls[*tab][m2];
211
        }
212
        scale+=6;
213
      }
214
      else {
215
        fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
216
        fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
217
      }
218
/*
219
   should we use individual scalefac for channel 2 or
220
   is the current way the right one , where we just copy channel 1 to
221
   channel 2 ??
222
   The current 'strange' thing is, that we throw away the scalefac
223
   values for the second channel ...!!
224
-> changed .. now we use the scalefac values of channel one !!
225
*/
226
    }
227
 
228
    if(sblimit > (fr->down_sample_sblimit) )
229
      sblimit = fr->down_sample_sblimit;
230
 
231
    for(i=sblimit;i
232
      for (j=0;j
233
        fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
234
 
235
}
236
 
237
static void II_select_table(struct frame *fr)
238
{
239
  static int translate[3][2][16] =
240
   { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
241
       { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
242
     { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
243
       { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
244
     { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
245
       { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
246
 
247
  int table,sblim;
248
  static struct al_table *tables[5] =
249
       { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 };
250
  static int sblims[5] = { 27 , 30 , 8, 12 , 30 };
251
 
252
  if(fr->sampling_frequency >= 3)	/* Or equivalent: (fr->lsf == 1) */
253
    table = 4;
254
  else
255
    table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index];
256
  sblim = sblims[table];
257
 
258
  fr->alloc      = tables[table];
259
  fr->II_sblimit = sblim;
260
}
261
 
262
 
263
int do_layer2(struct frame *fr,byte *pcm_sample,int *pcm_point)
264
{
265
  int clip=0;
266
  int i,j;
267
  int stereo = fr->stereo;
268
  real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
269
  unsigned int bit_alloc[64];
270
  int scale[192];
271
  int single = fr->single;
272
 
273
  II_select_table(fr);
274
  fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
275
     (fr->mode_ext<<2)+4 : fr->II_sblimit;
276
 
277
  if (fr->jsbound > fr->II_sblimit) {
278
	  fr->jsbound=fr->II_sblimit;
279
  }
280
 
281
  if(stereo == 1 || single == 3)
282
    single = 0;
283
 
284
  II_step_one(bit_alloc, scale, fr);
285
 
286
  for (i=0;i
287
  {
288
    II_step_two(bit_alloc,fraction,scale,fr,i>>2);
289
    for (j=0;j<3;j++)
290
    {
291
      if(single >= 0)
292
      {
293
        clip += (fr->synth_mono) (fraction[single][j],pcm_sample,pcm_point);
294
      }
295
      else {
296
        int p1=*pcm_point;
297
          clip += (fr->synth) (fraction[0][j],0,pcm_sample, &p1);
298
          clip += (fr->synth) (fraction[1][j],1,pcm_sample,pcm_point);
299
      }
300
    }
301
  }
302
 
303
  return clip;
304
}
305
 
306