~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpg123_artsplugin/mpg123/layer2.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

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