ReactOS  0.4.13-dev-92-gf251225
layer2.c
Go to the documentation of this file.
1 /*
2  layer2.c: the layer 2 decoder, root of mpg123
3 
4  copyright 1994-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  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 #ifndef NO_LAYER2
15 #include "l2tables.h"
16 #endif
17 #include "getbits.h"
18 
19 #ifndef NO_LAYER12 /* Stuff needed for layer I and II. */
20 
21 static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */
22 static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */
23 static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
24 
25 #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES)
26 #include "l12_integer_tables.h"
27 #else
28 static const double mulmul[27] =
29 {
30  0.0 , -2.0/3.0 , 2.0/3.0 ,
31  2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
32  2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
33  2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
34  -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
35  -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0
36 };
37 #endif
38 
39 void init_layer12(void)
40 {
41  const int base[3][9] =
42  {
43  { 1 , 0, 2 , } ,
44  { 17, 18, 0 , 19, 20 , } ,
45  { 21, 1, 22, 23, 0, 24, 25, 2, 26 }
46  };
47  int i,j,k,l,len;
48  const int tablen[3] = { 3 , 5 , 9 };
49  int *itable;
50  int *tables[3] = { grp_3tab , grp_5tab , grp_9tab };
51 
52  for(i=0;i<3;i++)
53  {
54  itable = tables[i];
55  len = tablen[i];
56  for(j=0;j<len;j++)
57  for(k=0;k<len;k++)
58  for(l=0;l<len;l++)
59  {
60  *itable++ = base[i][l];
61  *itable++ = base[i][k];
62  *itable++ = base[i][j];
63  }
64  }
65 }
66 
67 void init_layer12_stuff(mpg123_handle *fr, real* (*init_table)(mpg123_handle *fr, real *table, int m))
68 {
69  int k;
70  real *table;
71  for(k=0;k<27;k++)
72  {
73  table = init_table(fr, fr->muls[k], k);
74  *table++ = 0.0;
75  }
76 }
77 
79 {
80 #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES)
81  int i;
82  for(i=0;i<63;i++)
83  *table++ = layer12_table[m][i];
84 #else
85  int i,j;
86  for(j=3,i=0;i<63;i++,j--)
87  *table++ = DOUBLE_TO_REAL_SCALE_LAYER12(mulmul[m] * pow(2.0,(double) j / 3.0));
88 #endif
89 
90  return table;
91 }
92 
93 #ifdef OPT_MMXORSSE
95 {
96  int i,j;
97  if(!fr->p.down_sample)
98  {
99  for(j=3,i=0;i<63;i++,j--)
100  *table++ = DOUBLE_TO_REAL(16384 * mulmul[m] * pow(2.0,(double) j / 3.0));
101  }
102  else
103  {
104  for(j=3,i=0;i<63;i++,j--)
105  *table++ = DOUBLE_TO_REAL(mulmul[m] * pow(2.0,(double) j / 3.0));
106  }
107  return table;
108 }
109 #endif
110 
111 #endif /* NO_LAYER12 */
112 
113 /* The rest is the actual decoding of layer II data. */
114 
115 #ifndef NO_LAYER2
116 
117 static void II_step_one(unsigned int *bit_alloc,int *scale,mpg123_handle *fr)
118 {
119  int stereo = fr->stereo-1;
120  int sblimit = fr->II_sblimit;
121  int jsbound = fr->jsbound;
122  int sblimit2 = fr->II_sblimit<<stereo;
123  const struct al_table *alloc1 = fr->alloc;
124  int i;
125  unsigned int scfsi_buf[64];
126  unsigned int *scfsi,*bita;
127  int sc,step;
128 
129  bita = bit_alloc;
130  if(stereo)
131  {
132  for(i=jsbound;i;i--,alloc1+=(1<<step))
133  {
134  step=alloc1->bits;
135  *bita++ = (char) getbits(fr, step);
136  *bita++ = (char) getbits(fr, step);
137  }
138  for(i=sblimit-jsbound;i;i--,alloc1+=(1<<step))
139  {
140  step=alloc1->bits;
141  bita[0] = (char) getbits(fr, step);
142  bita[1] = bita[0];
143  bita+=2;
144  }
145  bita = bit_alloc;
146  scfsi=scfsi_buf;
147 
148  for(i=sblimit2;i;i--)
149  if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2);
150  }
151  else /* mono */
152  {
153  for(i=sblimit;i;i--,alloc1+=(1<<step))
154  {
155  step=alloc1->bits;
156  *bita++ = (char) getbits(fr, step);
157  }
158  bita = bit_alloc;
159  scfsi=scfsi_buf;
160  for(i=sblimit;i;i--)
161  if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2);
162  }
163 
164  bita = bit_alloc;
165  scfsi=scfsi_buf;
166  for(i=sblimit2;i;i--)
167  if(*bita++)
168  switch(*scfsi++)
169  {
170  case 0:
171  *scale++ = getbits_fast(fr, 6);
172  *scale++ = getbits_fast(fr, 6);
173  *scale++ = getbits_fast(fr, 6);
174  break;
175  case 1 :
176  *scale++ = sc = getbits_fast(fr, 6);
177  *scale++ = sc;
178  *scale++ = getbits_fast(fr, 6);
179  break;
180  case 2:
181  *scale++ = sc = getbits_fast(fr, 6);
182  *scale++ = sc;
183  *scale++ = sc;
184  break;
185  default: /* case 3 */
186  *scale++ = getbits_fast(fr, 6);
187  *scale++ = sc = getbits_fast(fr, 6);
188  *scale++ = sc;
189  break;
190  }
191 }
192 
193 
194 static void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,mpg123_handle *fr,int x1)
195 {
196  int i,j,k,ba;
197  int stereo = fr->stereo;
198  int sblimit = fr->II_sblimit;
199  int jsbound = fr->jsbound;
200  const struct al_table *alloc2,*alloc1 = fr->alloc;
201  unsigned int *bita=bit_alloc;
202  int d1,step;
203 
204  for(i=0;i<jsbound;i++,alloc1+=(1<<step))
205  {
206  step = alloc1->bits;
207  for(j=0;j<stereo;j++)
208  {
209  if( (ba=*bita++) )
210  {
211  k=(alloc2 = alloc1+ba)->bits;
212  if( (d1=alloc2->d) < 0)
213  {
214  real cm=fr->muls[k][scale[x1]];
215  fraction[j][0][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm);
216  fraction[j][1][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm);
217  fraction[j][2][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm);
218  }
219  else
220  {
221  const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
222  unsigned int idx,*tab,m=scale[x1];
223  idx = (unsigned int) getbits(fr, k);
224  tab = (unsigned int *) (table[d1] + idx + idx + idx);
225  fraction[j][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]);
226  fraction[j][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]);
227  fraction[j][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m]);
228  }
229  scale+=3;
230  }
231  else
232  fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = DOUBLE_TO_REAL(0.0);
233  }
234  }
235 
236  for(i=jsbound;i<sblimit;i++,alloc1+=(1<<step))
237  {
238  step = alloc1->bits;
239  bita++; /* channel 1 and channel 2 bitalloc are the same */
240  if( (ba=*bita++) )
241  {
242  k=(alloc2 = alloc1+ba)->bits;
243  if( (d1=alloc2->d) < 0)
244  {
245  real cm;
246  cm=fr->muls[k][scale[x1+3]];
247  fraction[0][0][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1);
248  fraction[0][1][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1);
249  fraction[0][2][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1);
250  fraction[1][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm);
251  fraction[1][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm);
252  fraction[1][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm);
253  cm=fr->muls[k][scale[x1]];
254  fraction[0][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm);
255  fraction[0][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm);
256  fraction[0][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm);
257  }
258  else
259  {
260  const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
261  unsigned int idx,*tab,m1,m2;
262  m1 = scale[x1]; m2 = scale[x1+3];
263  idx = (unsigned int) getbits(fr, k);
264  tab = (unsigned int *) (table[d1] + idx + idx + idx);
265  fraction[0][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]);
266  fraction[0][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]);
267  fraction[0][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m2]);
268  }
269  scale+=6;
270  }
271  else
272  {
273  fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
274  fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = DOUBLE_TO_REAL(0.0);
275  }
276 /*
277  Historic comment...
278  should we use individual scalefac for channel 2 or
279  is the current way the right one , where we just copy channel 1 to
280  channel 2 ??
281  The current 'strange' thing is, that we throw away the scalefac
282  values for the second channel ...!!
283  -> changed .. now we use the scalefac values of channel one !!
284 */
285  }
286 
287  if(sblimit > (fr->down_sample_sblimit) )
288  sblimit = fr->down_sample_sblimit;
289 
290  for(i=sblimit;i<SBLIMIT;i++)
291  for (j=0;j<stereo;j++)
292  fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = DOUBLE_TO_REAL(0.0);
293 }
294 
295 
297 {
298  const int translate[3][2][16] =
299  {
300  {
301  { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 },
302  { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 }
303  },
304  {
305  { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 },
306  { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 }
307  },
308  {
309  { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 },
310  { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 }
311  }
312  };
313 
314  int table,sblim;
315  const struct al_table *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 };
316  const int sblims[5] = { 27 , 30 , 8, 12 , 30 };
317 
318  if(fr->sampling_frequency >= 3) /* Or equivalent: (fr->lsf == 1) */
319  table = 4;
320  else
322 
323  sblim = sblims[table];
324  fr->alloc = tables[table];
325  fr->II_sblimit = sblim;
326 }
327 
328 
330 {
331  int clip=0;
332  int i,j;
333  int stereo = fr->stereo;
334  /* pick_table clears unused subbands */
335  /* replacement for real fraction[2][4][SBLIMIT], needs alignment. */
336  real (*fraction)[4][SBLIMIT] = fr->layer2.fraction;
337  unsigned int bit_alloc[64];
338  int scale[192];
339  int single = fr->single;
340 
341  II_select_table(fr);
342  fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : fr->II_sblimit;
343 
344  if(fr->jsbound > fr->II_sblimit)
345  {
346  fprintf(stderr, "Truncating stereo boundary to sideband limit.\n");
347  fr->jsbound=fr->II_sblimit;
348  }
349 
350  /* TODO: What happens with mono mixing, actually? */
351  if(stereo == 1 || single == SINGLE_MIX) /* also, mix not really handled */
353 
354  II_step_one(bit_alloc, scale, fr);
355 
356  for(i=0;i<SCALE_BLOCK;i++)
357  {
358  II_step_two(bit_alloc,fraction,scale,fr,i>>2);
359  for(j=0;j<3;j++)
360  {
361  if(single != SINGLE_STEREO)
362  clip += (fr->synth_mono)(fraction[single][j], fr);
363  else
364  clip += (fr->synth_stereo)(fraction[0][j], fraction[1][j], fr);
365  }
366  }
367 
368  return clip;
369 }
370 
371 #endif /* NO_LAYER2 */
void init_layer12_stuff(mpg123_handle *fr, real *(*init_table)(mpg123_handle *fr, real *table, int m))
Definition: layer2.c:67
struct mpg123_pars_struct p
Definition: frame.h:287
#define DOUBLE_TO_REAL(x)
#define init_layer12_table_mmx
Definition: intsym.h:159
#define SCALE_BLOCK
#define SINGLE_STEREO
Definition: frame.h:187
static const struct al_table alloc_1[]
Definition: l2tables.h:56
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:9032
_In_ CLIPOBJ _In_ BRUSHOBJ _In_ LONG x1
Definition: winddi.h:3706
#define SBLIMIT
_Tp _STLP_CALL real(const complex< _Tp > &__z)
Definition: _complex.h:727
GLenum GLsizei GLenum GLenum const GLvoid * table
Definition: glext.h:5644
static int grp_3tab[32 *3]
Definition: layer2.c:21
float single
Definition: msinkaut.idl:29
real * init_layer12_table(mpg123_handle *fr, real *table, int m)
Definition: layer2.c:78
const struct al_table * alloc
Definition: frame.h:177
#define REAL_SCALE_LAYER12(x)
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * bits
Definition: glext.h:10929
static void II_step_two(unsigned int *bit_alloc, real fraction[2][4][SBLIMIT], int *scale, mpg123_handle *fr, int x1)
Definition: layer2.c:194
const GLfloat * m
Definition: glext.h:10848
int down_sample_sblimit
Definition: frame.h:193
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define SINGLE_LEFT
Definition: frame.h:188
float pow(float __x, int __y)
Definition: _cmath.h:458
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
static const struct al_table alloc_4[]
Definition: l2tables.h:127
int sampling_frequency
Definition: frame.h:204
unsigned int idx
Definition: utils.c:41
unsigned char
Definition: typeof.h:29
int do_layer2(mpg123_handle *fr)
Definition: layer2.c:329
Definition: frame.h:32
static int grp_5tab[128 *3]
Definition: layer2.c:22
func_synth_mono synth_mono
Definition: frame.h:181
real muls[27][64]
Definition: frame.h:144
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
r l[0]
Definition: byte_order.h:167
#define MPG_MD_JOINT_STEREO
static void II_step_one(unsigned int *bit_alloc, int *scale, mpg123_handle *fr)
Definition: layer2.c:117
static void translate(POINT *pt, UINT count, const XFORM *xform)
Definition: metafile.c:2586
static int grp_9tab[1024 *3]
Definition: layer2.c:23
static void II_select_table(mpg123_handle *fr)
Definition: layer2.c:296
short d
Definition: frame.h:35
static const struct al_table alloc_0[]
Definition: l2tables.h:16
func_synth_stereo synth_stereo
Definition: frame.h:180
static const struct al_table alloc_3[]
Definition: l2tables.h:111
GLenum GLsizei len
Definition: glext.h:6722
#define REAL_MUL_SCALE_LAYER12(x, y)
#define DOUBLE_TO_REAL_15(x)
#define getbits_fast(fr, nob)
Definition: getbits.h:92
static const double mulmul[27]
Definition: layer2.c:28
struct mpg123_handle_struct::@3293 layer2
FILE * stderr
static const struct al_table alloc_2[]
Definition: l2tables.h:99
struct nls_table * tables
Definition: nls_base.c:22
#define DOUBLE_TO_REAL_SCALE_LAYER12(x)
int k
Definition: mpi.c:3369
#define SINGLE_MIX
Definition: frame.h:190
void init_layer12(void)
Definition: layer2.c:39
#define getbits
Definition: intsym.h:195
static const real layer12_table[27][64]
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31