Coverage Report

Created: 2026-06-30 07:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/contrib/contrib-build/mpg123/src/libmpg123/layer3.c
Line
Count
Source
1
/*
2
  layer3.c: the layer 3 decoder
3
4
  copyright 1995-2021 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
  Dear visitor:
9
  If you feel you don't understand fully the works of this file, your feeling might be correct.
10
11
  Optimize-TODO: put short bands into the band-field without the stride of 3 reals
12
  Length-optimze: unify long and short band code where it is possible
13
14
  The int-vs-pointer situation has to be cleaned up.
15
*/
16
17
#include "mpg123lib_intern.h"
18
#ifdef USE_NEW_HUFFTABLE
19
#include "newhuffman.h"
20
#else
21
#include "huffman.h"
22
#endif
23
#include "getbits.h"
24
#include "../common/debug.h"
25
26
27
/* Predeclare the assembly routines, only called from wrappers here. */
28
void INT123_dct36_3dnow   (real *,real *,real *,const real *,real *);
29
void INT123_dct36_3dnowext(real *,real *,real *,const real *,real *);
30
void INT123_dct36_x86_64  (real *,real *,real *,const real *,real *);
31
void INT123_dct36_sse     (real *,real *,real *,const real *,real *);
32
void INT123_dct36_avx     (real *,real *,real *,const real *,real *);
33
void INT123_dct36_neon    (real *,real *,real *,const real *,real *);
34
void INT123_dct36_neon64  (real *,real *,real *,const real *,real *);
35
36
/* define CUT_SFB21 if you want to cut-off the frequency above 16kHz */
37
#if 0
38
#define CUT_SFB21
39
#endif
40
41
#include "l3tabs.h"
42
#include "l3bandgain.h"
43
44
#ifdef RUNTIME_TABLES
45
#include "init_layer3.h"
46
#endif
47
48
/* Decoder state data, living on the stack of INT123_do_layer3. */
49
50
struct gr_info_s
51
{
52
  int scfsi;
53
  unsigned part2_3_length;
54
  unsigned big_values;
55
  unsigned scalefac_compress;
56
  unsigned block_type;
57
  unsigned mixed_block_flag;
58
  unsigned table_select[3];
59
  /* Making those two signed int as workaround for open64/pathscale/sun compilers, and also for consistency, since they're worked on together with other signed variables. */
60
  int maxband[3];
61
  int maxbandl;
62
  unsigned maxb;
63
  unsigned region1start;
64
  unsigned region2start;
65
  unsigned preflag;
66
  unsigned scalefac_scale;
67
  unsigned count1table_select;
68
#ifdef REAL_IS_FIXED
69
  const real *full_gain[3];
70
  const real *pow2gain;
71
#else
72
  real *full_gain[3];
73
  real *pow2gain;
74
#endif
75
};
76
77
struct III_sideinfo
78
{
79
  unsigned main_data_begin;
80
  unsigned private_bits;
81
  /* Hm, funny... struct inside struct... */
82
  struct { struct gr_info_s gr[2]; } ch[2];
83
};
84
85
#ifdef OPT_MMXORSSE
86
real INT123_init_layer3_gainpow2_mmx(mpg123_handle *fr, int i)
87
0
{
88
0
  if(!fr->p.down_sample) return DOUBLE_TO_REAL(16384.0 * pow((double)2.0,-0.25 * (double) (i+210) ));
89
0
  else return DOUBLE_TO_REAL(pow((double)2.0,-0.25 * (double) (i+210)));
90
0
}
91
#endif
92
93
real INT123_init_layer3_gainpow2(mpg123_handle *fr, int i)
94
101M
{
95
101M
  return DOUBLE_TO_REAL_SCALE_LAYER3(pow((double)2.0,-0.25 * (double) (i+210)),i+256);
96
101M
}
97
98
void INT123_init_layer3_stuff(mpg123_handle *fr, real (*gainpow2_func)(mpg123_handle *fr, int i))
99
267k
{
100
267k
  int i,j;
101
102
#ifdef REAL_IS_FIXED
103
  fr->gainpow2 = gainpow2;
104
#else
105
101M
  for(i=-256;i<118+4;i++)
106
101M
    fr->gainpow2[i+256] = gainpow2_func(fr,i);
107
267k
#endif
108
109
2.67M
  for(j=0;j<9;j++)
110
2.40M
  {
111
57.7M
    for(i=0;i<23;i++)
112
55.3M
    {
113
55.3M
      fr->longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
114
55.3M
      if(fr->longLimit[j][i] > (fr->down_sample_sblimit) )
115
4.09M
      fr->longLimit[j][i] = fr->down_sample_sblimit;
116
55.3M
    }
117
36.0M
    for(i=0;i<14;i++)
118
33.6M
    {
119
33.6M
      fr->shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
120
33.6M
      if(fr->shortLimit[j][i] > (fr->down_sample_sblimit) )
121
771k
      fr->shortLimit[j][i] = fr->down_sample_sblimit;
122
33.6M
    }
123
2.40M
  }
124
267k
}
125
126
/*
127
  Observe!
128
  Now come the actualy decoding routines.
129
*/
130
131
/* read additional side information (for MPEG 1 and MPEG 2) */
132
static int III_get_side_info(mpg123_handle *fr, struct III_sideinfo *si,int stereo, int ms_stereo,long sfreq,int single)
133
559k
{
134
559k
  int ch, gr;
135
559k
  int powdiff = (single == SINGLE_MIX) ? 4 : 0;
136
137
559k
  const int tabs[2][5] = { { 2,9,5,3,4 } , { 1,8,1,2,9 } };
138
559k
  const int *tab = tabs[fr->hdr.lsf];
139
140
559k
  { /* First ensure we got enough bits available. */
141
559k
    unsigned int needbits = 0;
142
559k
    needbits += tab[1]; /* main_data_begin */
143
559k
    needbits += stereo == 1 ? tab[2] : tab[3]; /* private */
144
559k
    if(!fr->hdr.lsf)
145
251k
      needbits += stereo*4; /* scfsi */
146
    /* For each granule for each channel ... */
147
559k
    needbits += tab[0]*stereo*(29+tab[4]+1+22+(!fr->hdr.lsf?1:0)+2);
148
559k
    if(fr->bits_avail < needbits) \
149
0
    {
150
0
      if(NOQUIET)
151
0
        error2( "%u bits for side info needed, only %li available"
152
0
        , needbits, fr->bits_avail );
153
0
      return 1;
154
0
    }
155
559k
  }
156
157
559k
  si->main_data_begin = getbits(fr, tab[1]);
158
159
559k
  if(si->main_data_begin > fr->bitreservoir)
160
83.3k
  {
161
83.3k
    if(!fr->to_ignore && VERBOSE2) fprintf(stderr, "Note: missing %d bytes in bit reservoir for frame %li\n", (int)(si->main_data_begin - fr->bitreservoir), (long)fr->num);
162
163
    /*  overwrite main_data_begin for the really available bit reservoir */
164
83.3k
    backbits(fr, tab[1]);
165
83.3k
    if(fr->hdr.lsf == 0)
166
27.8k
    {
167
27.8k
      fr->wordpointer[0] = (unsigned char) (fr->bitreservoir >> 1);
168
27.8k
      fr->wordpointer[1] = (unsigned char) ((fr->bitreservoir & 1) << 7);
169
27.8k
    }
170
55.5k
    else fr->wordpointer[0] = (unsigned char) fr->bitreservoir;
171
172
    /* zero "side-info" data for a silence-frame
173
    without touching audio data used as bit reservoir for following frame */
174
83.3k
    memset(fr->wordpointer+2, 0, fr->hdr.ssize-2);
175
176
    /* reread the new bit reservoir offset */
177
83.3k
    si->main_data_begin = getbits(fr, tab[1]);
178
83.3k
  }
179
180
  /* Keep track of the available data bytes for the bit reservoir.
181
     CRC is included in ssize already. */
182
559k
  fr->bitreservoir = fr->bitreservoir + fr->hdr.framesize - fr->hdr.ssize;
183
184
  /* Limit the reservoir to the max for MPEG 1.0 or 2.x . */
185
559k
  if(fr->bitreservoir > (unsigned int) (fr->hdr.lsf == 0 ? 511 : 255))
186
291k
  fr->bitreservoir = (fr->hdr.lsf == 0 ? 511 : 255);
187
188
  /* Now back into less commented territory. It's code. It works. */
189
190
559k
  if (stereo == 1)
191
279k
  si->private_bits = getbits(fr, tab[2]);
192
280k
  else 
193
280k
  si->private_bits = getbits(fr, tab[3]);
194
195
631k
  if(!fr->hdr.lsf) for(ch=0; ch<stereo; ch++)
196
379k
  {
197
379k
    si->ch[ch].gr[0].scfsi = -1;
198
379k
    si->ch[ch].gr[1].scfsi = getbits(fr, 4);
199
379k
  }
200
201
1.34M
  for (gr=0; gr<tab[0]; gr++)
202
2.00M
  for (ch=0; ch<stereo; ch++)
203
1.21M
  {
204
1.21M
    register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
205
1.21M
    unsigned int qss;
206
1.21M
    gr_info->part2_3_length = getbits(fr, 12);
207
1.21M
    gr_info->big_values = getbits(fr, 9);
208
1.21M
    if(gr_info->big_values > 288)
209
186k
    {
210
186k
      if(NOQUIET) error("big_values too large!");
211
186k
      gr_info->big_values = 288;
212
186k
    }
213
1.21M
    qss = getbits_fast(fr, 8);
214
1.21M
    gr_info->pow2gain = fr->gainpow2+256 - qss + powdiff;
215
1.21M
    if(ms_stereo)
216
270k
      gr_info->pow2gain += 2;
217
1.21M
#ifndef NO_MOREINFO
218
1.21M
    if(fr->pinfo)
219
0
      fr->pinfo->qss[gr][ch] = qss;
220
1.21M
#endif
221
1.21M
    gr_info->scalefac_compress = getbits(fr, tab[4]);
222
1.21M
    if(gr_info->part2_3_length == 0)
223
434k
    {
224
434k
      if(gr_info->scalefac_compress > 0 && VERBOSE2)
225
434k
        error1( "scalefac_compress should be zero instead of %i"
226
434k
        , gr_info->scalefac_compress );
227
434k
      gr_info->scalefac_compress = 0;
228
434k
    }
229
230
    /* 22 bits for if/else block */
231
1.21M
    if(getbits(fr,1))
232
408k
    { /* window switch flag  */
233
408k
      int i;
234
408k
      gr_info->block_type       = getbits_fast(fr, 2);
235
408k
      gr_info->mixed_block_flag = get1bit(fr);
236
408k
      gr_info->table_select[0]  = getbits_fast(fr, 5);
237
408k
      gr_info->table_select[1]  = getbits_fast(fr, 5);
238
      /*
239
        table_select[2] not needed, because there is no region2,
240
        but to satisfy some verification tools we set it either.
241
      */
242
408k
      gr_info->table_select[2] = 0;
243
1.63M
      for(i=0;i<3;i++)
244
1.22M
      {
245
1.22M
        unsigned int sbg = (getbits_fast(fr, 3)<<3);
246
1.22M
        gr_info->full_gain[i] = gr_info->pow2gain + sbg;
247
1.22M
#ifndef NO_MOREINFO
248
1.22M
        if(fr->pinfo)
249
0
          fr->pinfo->sub_gain[gr][ch][i] = sbg / 8;
250
1.22M
#endif
251
1.22M
      }
252
253
408k
      if(gr_info->block_type == 0)
254
20.2k
      {
255
20.2k
        if(NOQUIET) error("Blocktype == 0 and window-switching == 1 not allowed.");
256
20.2k
        return 1;
257
20.2k
      }
258
259
      /* region_count/start parameters are implicit in this case. */       
260
388k
      if( (!fr->hdr.lsf || (gr_info->block_type == 2)) && !fr->hdr.mpeg25)
261
228k
      {
262
228k
        gr_info->region1start = 36>>1;
263
228k
        gr_info->region2start = 576>>1;
264
228k
      }
265
159k
      else
266
159k
      {
267
159k
        if(fr->hdr.mpeg25)
268
51.6k
        { 
269
51.6k
          int r0c,r1c;
270
51.6k
          if((gr_info->block_type == 2) && (!gr_info->mixed_block_flag) ) r0c = 5;
271
39.3k
          else r0c = 7;
272
273
          /* r0c+1+r1c+1 == 22, always. */
274
51.6k
          r1c = 20 - r0c;
275
51.6k
          gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
276
51.6k
          gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; 
277
51.6k
        }
278
108k
        else
279
108k
        {
280
108k
          gr_info->region1start = 54>>1;
281
108k
          gr_info->region2start = 576>>1; 
282
108k
        } 
283
159k
      }
284
388k
    }
285
808k
    else
286
808k
    {
287
808k
      int i,r0c,r1c;
288
3.23M
      for (i=0; i<3; i++)
289
2.42M
      gr_info->table_select[i] = getbits_fast(fr, 5);
290
291
808k
      r0c = getbits_fast(fr, 4); /* 0 .. 15 */
292
808k
      r1c = getbits_fast(fr, 3); /* 0 .. 7 */
293
808k
      gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
294
295
      /* max(r0c+r1c+2) = 15+7+2 = 24 */
296
808k
      if(r0c+1+r1c+1 > 22) gr_info->region2start = 576>>1;
297
795k
      else gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
298
299
808k
      gr_info->block_type = 0;
300
808k
      gr_info->mixed_block_flag = 0;
301
808k
    }
302
1.19M
    if(!fr->hdr.lsf) gr_info->preflag = get1bit(fr);
303
304
1.19M
    gr_info->scalefac_scale = get1bit(fr);
305
1.19M
    gr_info->count1table_select = get1bit(fr);
306
1.19M
  }
307
539k
  return 0;
308
559k
}
309
310
311
/* read scalefactors */
312
static int III_get_scale_factors_1(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int ch,int gr)
313
513k
{
314
513k
  const unsigned char slen[2][16] =
315
513k
  {
316
513k
    {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
317
513k
    {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
318
513k
  };
319
513k
  int numbits;
320
513k
  int num0 = slen[0][gr_info->scalefac_compress];
321
513k
  int num1 = slen[1][gr_info->scalefac_compress];
322
323
513k
  if(gr_info->block_type == 2)
324
50.1k
  {
325
50.1k
    int i=18;
326
50.1k
    numbits = (num0 + num1) * 18 /* num0 * (17+1?) + num1 * 18 */
327
50.1k
            - (gr_info->mixed_block_flag ? num0 : 0);
328
50.1k
    if(fr->bits_avail < numbits)
329
2.25k
      return -1;
330
331
47.9k
    if(gr_info->mixed_block_flag)
332
26.5k
    {
333
239k
      for (i=8;i;i--)
334
212k
      *scf++ = getbits_fast(fr, num0);
335
336
26.5k
      i = 9;
337
26.5k
    }
338
339
671k
    for(;i;i--) *scf++ = getbits_fast(fr, num0);
340
341
910k
    for(i = 18; i; i--) *scf++ = getbits_fast(fr, num1);
342
343
47.9k
    *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
344
47.9k
  }
345
462k
  else
346
462k
  {
347
462k
    int i;
348
462k
    int scfsi = gr_info->scfsi;
349
350
462k
    if(scfsi < 0)
351
305k
    { /* scfsi < 0 => granule == 0 */
352
305k
      numbits = (num0 + num1) * 10 + num0;
353
305k
      if(fr->bits_avail < numbits)
354
1.34k
        return -1;
355
356
3.65M
      for(i=11;i;i--) *scf++ = getbits_fast(fr, num0);
357
358
3.34M
      for(i=10;i;i--) *scf++ = getbits_fast(fr, num1);
359
360
304k
      *scf++ = 0;
361
304k
    }
362
157k
    else
363
157k
    {
364
157k
      numbits = !(scfsi & 0x8) * num0 * 6
365
157k
              + !(scfsi & 0x4) * num0 * 5
366
157k
              + !(scfsi & 0x2) * num1 * 5
367
157k
              + !(scfsi & 0x1) * num1 * 5;
368
157k
      if(fr->bits_avail < numbits)
369
1.70k
        return -1;
370
371
155k
      if(!(scfsi & 0x8))
372
123k
      {
373
867k
        for (i=0;i<6;i++) *scf++ = getbits_fast(fr, num0);
374
123k
      }
375
31.4k
      else scf += 6; 
376
377
155k
      if(!(scfsi & 0x4))
378
127k
      {
379
762k
        for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num0);
380
127k
      }
381
28.3k
      else scf += 5;
382
383
155k
      if(!(scfsi & 0x2))
384
129k
      {
385
775k
        for(i=0;i<5;i++) *scf++ = getbits_fast(fr, num1);
386
129k
      }
387
26.1k
      else scf += 5;
388
389
155k
      if(!(scfsi & 0x1))
390
141k
      {
391
848k
        for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num1);
392
141k
      }
393
14.0k
      else scf += 5;
394
395
155k
      *scf++ = 0;  /* no l[21] in original sources */
396
155k
    }
397
462k
  }
398
399
507k
  return numbits;
400
513k
}
401
402
403
static int III_get_scale_factors_2(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int i_stereo)
404
272k
{
405
272k
  const unsigned char *pnt;
406
272k
  int i,j,n=0,numbits=0;
407
272k
  unsigned int slen, slen2;
408
409
272k
  const unsigned char stab[3][6][4] =
410
272k
  {
411
272k
    {
412
272k
      { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0},
413
272k
      { 7, 7, 7,0 } , { 6, 6, 6,3 } , {  8, 8,5,0}
414
272k
    },
415
272k
    {
416
272k
      { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0},
417
272k
      {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0}
418
272k
    },
419
272k
    {
420
272k
      { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0},
421
272k
      { 6,15,12,0 } , { 6,12, 9,6 } , {  6,18,9,0}
422
272k
    }
423
272k
  }; 
424
425
272k
  if(i_stereo) /* i_stereo AND second channel -> INT123_do_layer3() checks this */
426
28.8k
  slen = i_slen2[gr_info->scalefac_compress>>1];
427
243k
  else
428
243k
  slen = n_slen2[gr_info->scalefac_compress];
429
430
272k
  gr_info->preflag = (slen>>15) & 0x1;
431
432
272k
  n = 0;  
433
272k
  if( gr_info->block_type == 2 )
434
27.4k
  {
435
27.4k
    n++;
436
27.4k
    if(gr_info->mixed_block_flag) n++;
437
27.4k
  }
438
439
272k
  pnt = stab[n][(slen>>12)&0x7];
440
441
272k
  slen2 = slen;
442
1.36M
  for(i=0;i<4;i++)
443
1.08M
  {
444
1.08M
    int num = slen2 & 0x7;
445
1.08M
    slen2 >>= 3;
446
1.08M
    if(num)
447
415k
      numbits += pnt[i] * num;
448
1.08M
  }
449
272k
  if(numbits > gr_info->part2_3_length)
450
12.4k
    return -1;
451
452
1.30M
  for(i=0;i<4;i++)
453
1.04M
  {
454
1.04M
    int num = slen & 0x7;
455
1.04M
    slen >>= 3;
456
1.04M
    if(num)
457
371k
    {
458
2.94M
      for(j=0;j<(int)(pnt[i]);j++) *scf++ = getbits_fast(fr, num);
459
371k
    }
460
668k
    else
461
3.93M
    for(j=0;j<(int)(pnt[i]);j++) *scf++ = 0;
462
1.04M
  }
463
464
260k
  n = (n << 1) + 1;
465
597k
  for(i=0;i<n;i++) *scf++ = 0;
466
467
260k
  return numbits;
468
272k
}
469
470
static unsigned char pretab_choice[2][22] =
471
{
472
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
473
  {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0}
474
};
475
476
/*
477
  Dequantize samples
478
  ...includes Huffman decoding
479
*/
480
481
/* 24 is enough because tab13 has max. a 19 bit huffvector */
482
/* The old code played games with shifting signed integers around in not quite */
483
/* legal ways. Also, it used long where just 32 bits are required. This could */
484
/* be good or bad on 64 bit architectures ... anyway, making clear that */
485
/* 32 bits suffice is a benefit. */
486
#if 0
487
/* To reconstruct old code, use this: */
488
#define MASK_STYPE long
489
#define MASK_UTYPE unsigned long
490
#define MASK_TYPE MASK_STYPE
491
#define MSB_MASK (mask < 0)
492
#else
493
/* This should be more proper: */
494
52.3M
#define MASK_STYPE int32_t
495
474k
#define MASK_UTYPE uint32_t
496
474k
#define MASK_TYPE  MASK_UTYPE
497
107M
#define MSB_MASK ((MASK_UTYPE)mask & (MASK_UTYPE)1<<(sizeof(MASK_TYPE)*8-1))
498
#endif
499
293M
#define BITSHIFT ((sizeof(MASK_TYPE)-1)*8)
500
#define REFRESH_MASK \
501
139M
  while(num < BITSHIFT) { \
502
56.5M
    mask |= ((MASK_UTYPE)getbyte(fr))<<(BITSHIFT-num); \
503
56.5M
    num += 8; \
504
56.5M
    part2remain -= 8; }
505
/* Complicated way of checking for msb value. This used to be (mask < 0). */
506
507
static int III_dequantize_sample(mpg123_handle *fr, real xr[SBLIMIT][SSLIMIT],int *scf, struct gr_info_s *gr_info,int sfreq,int part2bits)
508
767k
{
509
767k
  int shift = 1 + gr_info->scalefac_scale;
510
  // Pointer cast to make pedantic compilers happy.
511
767k
  real *xrpnt = (real*)xr;
512
  // Some compiler freaks out over &xr[SBLIMIT][0], which is the same.
513
767k
  real *xrpntlimit = (real*)xr+SBLIMIT*SSLIMIT;
514
767k
  int l[3],l3;
515
767k
  int part2remain = gr_info->part2_3_length - part2bits;
516
767k
  const short *me;
517
#ifdef REAL_IS_FIXED
518
  int gainpow2_scale_idx = 378;
519
#endif
520
521
  /* Assumption: If there is some part2_3_length at all, there should be
522
     enough of it to work with properly. In case of zero length we silently
523
     zero things. */
524
767k
  if(gr_info->part2_3_length > 0)
525
474k
  {
526
527
  /* mhipp tree has this split up a bit... */
528
474k
  int num=getbitoffset(fr);
529
474k
  MASK_TYPE mask;
530
  /* We must split this, because for num==0 the shift is undefined if you do it in one step. */
531
474k
  mask  = ((MASK_UTYPE) getbits(fr, num))<<BITSHIFT;
532
474k
  mask <<= 8-num;
533
474k
  part2remain -= num;
534
535
  /* Bitindex is zero now, we are allowed to use getbyte(). */
536
537
474k
  {
538
474k
    int bv       = gr_info->big_values;
539
474k
    int region1  = gr_info->region1start;
540
474k
    int region2  = gr_info->region2start;
541
474k
    l3 = ((576>>1)-bv)>>1;   
542
543
    /* we may lose the 'odd' bit here !! check this later again */
544
474k
    if(bv <= region1)
545
78.2k
    {
546
78.2k
      l[0] = bv;
547
78.2k
      l[1] = 0;
548
78.2k
      l[2] = 0;
549
78.2k
    }
550
395k
    else
551
395k
    {
552
395k
      l[0] = region1;
553
395k
      if(bv <= region2)
554
178k
      {
555
178k
        l[1] = bv - l[0];
556
178k
        l[2] = 0;
557
178k
      }
558
217k
      else
559
217k
      {
560
217k
        l[1] = region2 - l[0];
561
217k
        l[2] = bv - region2;
562
217k
      }
563
395k
    }
564
474k
  }
565
566
169M
#define CHECK_XRPNT if(xrpnt >= xrpntlimit) \
567
0
{ \
568
0
  if(NOQUIET) \
569
0
    error2("attempted xrpnt overflow (%p !< %p)", (void*) xrpnt, (void*) xrpntlimit); \
570
0
  return 1; \
571
0
}
572
573
474k
  if(gr_info->block_type == 2)
574
52.3k
  {
575
    /* decoding with short or mixed mode BandIndex table */
576
52.3k
    int i,max[4];
577
52.3k
    int step=0,lwin=3,cb=0;
578
52.3k
    register real v = 0.0;
579
52.3k
    register int mc;
580
52.3k
    register const short *m;
581
582
52.3k
    if(gr_info->mixed_block_flag)
583
17.6k
    {
584
17.6k
      max[3] = -1;
585
17.6k
      max[0] = max[1] = max[2] = 2;
586
17.6k
      m = map[sfreq][0];
587
17.6k
      me = mapend[sfreq][0];
588
17.6k
    }
589
34.6k
    else
590
34.6k
    {
591
34.6k
      max[0] = max[1] = max[2] = max[3] = -1;
592
      /* max[3] not really needed in this case */
593
34.6k
      m = map[sfreq][1];
594
34.6k
      me = mapend[sfreq][1];
595
34.6k
    }
596
597
52.3k
    mc = 0;
598
156k
    for(i=0;i<2;i++)
599
104k
    {
600
104k
      int lp = l[i];
601
104k
      const struct newhuff *h = ht+gr_info->table_select[i];
602
6.31M
      for(;lp;lp--,mc--)
603
6.21M
      {
604
6.21M
        register MASK_STYPE x,y;
605
6.21M
        if( (!mc) )
606
1.06M
        {
607
1.06M
          mc    = *m++;
608
//fprintf(stderr, "%i setting xrpnt = xr + %i (%ld)\n", __LINE__, *m, xrpnt-(real*)xr);
609
1.06M
          xrpnt = ((real *) xr) + (*m++);
610
1.06M
          lwin  = *m++;
611
1.06M
          cb    = *m++;
612
1.06M
          if(lwin == 3)
613
65.1k
          {
614
#ifdef REAL_IS_FIXED
615
            gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
616
#endif
617
65.1k
            v = gr_info->pow2gain[(*scf++) << shift];
618
65.1k
            step = 1;
619
65.1k
          }
620
1.00M
          else
621
1.00M
          {
622
#ifdef REAL_IS_FIXED
623
            gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2);
624
#endif
625
1.00M
            v = gr_info->full_gain[lwin][(*scf++) << shift];
626
1.00M
            step = 3;
627
1.00M
          }
628
1.06M
        }
629
6.21M
        {
630
6.21M
          const short *val = h->table;
631
6.21M
          REFRESH_MASK;
632
6.21M
#ifdef USE_NEW_HUFFTABLE
633
9.07M
          while((y=val[(MASK_UTYPE)mask>>(BITSHIFT+4)])<0)
634
2.85M
          {
635
2.85M
            val -= y;
636
2.85M
            num -= 4;
637
2.85M
            mask <<= 4;
638
2.85M
          }
639
6.21M
          num -= (y >> 8);
640
6.21M
          mask <<= (y >> 8);
641
6.21M
          x = (y >> 4) & 0xf;
642
6.21M
          y &= 0xf;
643
#else
644
          while((y=*val++)<0)
645
          {
646
            if (MSB_MASK) val -= y;
647
648
            num--;
649
            mask <<= 1;
650
          }
651
          x = y >> 4;
652
          y &= 0xf;
653
#endif
654
6.21M
        }
655
6.21M
        CHECK_XRPNT;
656
6.21M
        if(x == 15 && h->linbits)
657
99.3k
        {
658
99.3k
          max[lwin] = cb;
659
99.3k
          REFRESH_MASK;
660
99.3k
          x += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits);
661
99.3k
          num -= h->linbits+1;
662
99.3k
          mask <<= h->linbits;
663
99.3k
          if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
664
47.9k
          else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
665
666
99.3k
          mask <<= 1;
667
99.3k
        }
668
6.11M
        else if(x)
669
2.07M
        {
670
2.07M
          max[lwin] = cb;
671
2.07M
          if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
672
1.59M
          else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
673
674
2.07M
          num--;
675
2.07M
          mask <<= 1;
676
2.07M
        }
677
4.03M
        else *xrpnt = DOUBLE_TO_REAL(0.0);
678
679
6.21M
        xrpnt += step;
680
6.21M
        CHECK_XRPNT;
681
6.21M
        if(y == 15 && h->linbits)
682
959k
        {
683
959k
          max[lwin] = cb;
684
959k
          REFRESH_MASK;
685
959k
          y += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits);
686
959k
          num -= h->linbits+1;
687
959k
          mask <<= h->linbits;
688
959k
          if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
689
906k
          else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
690
691
959k
          mask <<= 1;
692
959k
        }
693
5.25M
        else if(y)
694
1.14M
        {
695
1.14M
          max[lwin] = cb;
696
1.14M
          if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
697
687k
          else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
698
699
1.14M
          num--;
700
1.14M
          mask <<= 1;
701
1.14M
        }
702
4.11M
        else *xrpnt = DOUBLE_TO_REAL(0.0);
703
704
6.21M
        xrpnt += step;
705
6.21M
      }
706
104k
    }
707
708
2.03M
    for(;l3 && (part2remain+num > 0);l3--)
709
1.99M
    {
710
1.99M
      const struct newhuff* h;
711
1.99M
      const short* val;
712
1.99M
      register short a;
713
714
1.99M
      h = htc+gr_info->count1table_select;
715
1.99M
      val = h->table;
716
717
1.99M
      REFRESH_MASK;
718
8.42M
      while((a=*val++)<0)
719
6.42M
      {
720
6.42M
        if(MSB_MASK) val -= a;
721
722
6.42M
        num--;
723
6.42M
        mask <<= 1;
724
6.42M
      }
725
1.99M
      if(part2remain+num <= 0)
726
17.7k
      {
727
17.7k
        num -= part2remain+num;
728
17.7k
        break;
729
17.7k
      }
730
731
9.88M
      for(i=0;i<4;i++)
732
7.91M
      {
733
7.91M
        if(!(i & 1))
734
3.95M
        {
735
3.95M
          if(!mc)
736
447k
          {
737
447k
            mc = *m++;
738
//fprintf(stderr, "%i setting xrpnt = xr + %i (%ld)\n", __LINE__, *m, xrpnt-(real*)xr);
739
447k
            xrpnt = ((real *) xr) + (*m++);
740
447k
            lwin = *m++;
741
447k
            cb = *m++;
742
447k
            if(lwin == 3)
743
39.6k
            {
744
#ifdef REAL_IS_FIXED
745
              gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
746
#endif
747
39.6k
              v = gr_info->pow2gain[(*scf++) << shift];
748
39.6k
              step = 1;
749
39.6k
            }
750
407k
            else
751
407k
            {
752
#ifdef REAL_IS_FIXED
753
              gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2);
754
#endif
755
407k
              v = gr_info->full_gain[lwin][(*scf++) << shift];
756
407k
              step = 3;
757
407k
            }
758
447k
          }
759
3.95M
          mc--;
760
3.95M
        }
761
7.91M
        CHECK_XRPNT;
762
7.91M
        if( (a & (0x8>>i)) )
763
2.82M
        {
764
2.82M
          max[lwin] = cb;
765
2.82M
          if(part2remain+num <= 0)
766
5.83k
          break;
767
768
2.81M
          if(MSB_MASK) *xrpnt = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
769
2.34M
          else         *xrpnt =  REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
770
771
2.81M
          num--;
772
2.81M
          mask <<= 1;
773
2.81M
        }
774
5.09M
        else *xrpnt = DOUBLE_TO_REAL(0.0);
775
776
7.90M
        xrpnt += step;
777
7.90M
      }
778
1.97M
    }
779
780
52.3k
    if(lwin < 3)
781
44.7k
    { /* short band? */
782
64.5k
      while(1)
783
64.5k
      {
784
539k
        for(;mc > 0;mc--)
785
475k
        {
786
475k
          CHECK_XRPNT;
787
475k
          *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3; /* short band -> step=3 */
788
475k
          *xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3;
789
475k
        }
790
64.5k
        if(m >= me)
791
28.3k
        break;
792
793
36.1k
        mc    = *m++;
794
36.1k
        xrpnt = ((real *) xr) + *m++;
795
36.1k
        if(*m++ == 0)
796
16.3k
        break; /* optimize: field will be set to zero at the end of the function */
797
798
19.8k
        m++; /* cb */
799
19.8k
      }
800
44.7k
    }
801
802
52.3k
    gr_info->maxband[0] = max[0]+1;
803
52.3k
    gr_info->maxband[1] = max[1]+1;
804
52.3k
    gr_info->maxband[2] = max[2]+1;
805
52.3k
    gr_info->maxbandl   = max[3]+1;
806
807
52.3k
    {
808
52.3k
      int rmax = max[0] > max[1] ? max[0] : max[1];
809
52.3k
      rmax = (rmax > max[2] ? rmax : max[2]) + 1;
810
52.3k
      gr_info->maxb = rmax ? fr->shortLimit[sfreq][rmax] : fr->longLimit[sfreq][max[3]+1];
811
52.3k
    }
812
813
52.3k
  }
814
421k
  else
815
421k
  {
816
    /* decoding with 'long' BandIndex table (block_type != 2) */
817
421k
    const unsigned char *pretab = pretab_choice[gr_info->preflag];
818
421k
    int i,max = -1;
819
421k
    int cb = 0;
820
421k
    const short *m = map[sfreq][2];
821
421k
    register real v = 0.0;
822
421k
    int mc = 0;
823
824
    /* long hash table values */
825
1.68M
    for(i=0;i<3;i++)
826
1.26M
    {
827
1.26M
      int lp = l[i];
828
1.26M
      const struct newhuff *h = ht+gr_info->table_select[i];
829
830
53.5M
      for(;lp;lp--,mc--)
831
52.3M
      {
832
52.3M
        MASK_STYPE x,y;
833
52.3M
        if(!mc)
834
6.56M
        {
835
6.56M
          mc = *m++;
836
6.56M
          cb = *m++;
837
#ifdef CUT_SFB21
838
          if(cb == 21)
839
            v = 0.0;
840
          else
841
#endif
842
6.56M
          {
843
#ifdef REAL_IS_FIXED
844
            gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
845
#endif
846
6.56M
            v = gr_info->pow2gain[(*(scf++) + (*pretab++)) << shift];
847
6.56M
          }
848
6.56M
        }
849
52.3M
        {
850
52.3M
          const short *val = h->table;
851
52.3M
          REFRESH_MASK;
852
52.3M
#ifdef USE_NEW_HUFFTABLE
853
75.8M
          while((y=val[(MASK_UTYPE)mask>>(BITSHIFT+4)])<0)
854
23.5M
          {
855
23.5M
            val -= y;
856
23.5M
            num -= 4;
857
23.5M
            mask <<= 4;
858
23.5M
          }
859
52.3M
          num -= (y >> 8);
860
52.3M
          mask <<= (y >> 8);
861
52.3M
          x = (y >> 4) & 0xf;
862
52.3M
          y &= 0xf;
863
#else
864
          while((y=*val++)<0)
865
          {
866
            if (MSB_MASK) val -= y;
867
868
            num--;
869
            mask <<= 1;
870
          }
871
          x = y >> 4;
872
          y &= 0xf;
873
#endif
874
52.3M
        }
875
876
52.3M
        CHECK_XRPNT;
877
52.3M
        if(x == 15 && h->linbits)
878
618k
        {
879
618k
          max = cb;
880
618k
          REFRESH_MASK;
881
618k
          x += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits);
882
618k
          num -= h->linbits+1;
883
618k
          mask <<= h->linbits;
884
618k
          if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
885
310k
          else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
886
887
618k
          mask <<= 1;
888
618k
        }
889
51.7M
        else if(x)
890
23.4M
        {
891
23.4M
          max = cb;
892
23.4M
          if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
893
16.9M
          else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
894
23.4M
          num--;
895
896
23.4M
          mask <<= 1;
897
23.4M
        }
898
28.2M
        else *xrpnt++ = DOUBLE_TO_REAL(0.0);
899
900
52.3M
        CHECK_XRPNT;
901
52.3M
        if(y == 15 && h->linbits)
902
10.0M
        {
903
10.0M
          max = cb;
904
10.0M
          REFRESH_MASK;
905
10.0M
          y += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits);
906
10.0M
          num -= h->linbits+1;
907
10.0M
          mask <<= h->linbits;
908
10.0M
          if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
909
9.63M
          else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
910
911
10.0M
          mask <<= 1;
912
10.0M
        }
913
42.3M
        else if(y)
914
13.9M
        {
915
13.9M
          max = cb;
916
13.9M
          if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
917
7.51M
          else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
918
919
13.9M
          num--;
920
13.9M
          mask <<= 1;
921
13.9M
        }
922
28.3M
        else *xrpnt++ = DOUBLE_TO_REAL(0.0);
923
52.3M
      }
924
1.26M
    }
925
926
    /* short (count1table) values */
927
11.4M
    for(;l3 && (part2remain+num > 0);l3--)
928
11.1M
    {
929
11.1M
      const struct newhuff *h = htc+gr_info->count1table_select;
930
11.1M
      const short *val = h->table;
931
11.1M
      register short a;
932
933
11.1M
      REFRESH_MASK;
934
44.2M
      while((a=*val++)<0)
935
33.1M
      {
936
33.1M
        if (MSB_MASK) val -= a;
937
938
33.1M
        num--;
939
33.1M
        mask <<= 1;
940
33.1M
      }
941
11.1M
      if(part2remain+num <= 0)
942
42.4k
      {
943
42.4k
        num -= part2remain+num;
944
42.4k
        break;
945
42.4k
      }
946
947
55.2M
      for(i=0;i<4;i++)
948
44.2M
      {
949
44.2M
        if(!(i & 1))
950
22.1M
        {
951
22.1M
          if(!mc)
952
1.32M
          {
953
1.32M
            mc = *m++;
954
1.32M
            cb = *m++;
955
#ifdef CUT_SFB21
956
            if(cb == 21)
957
              v = 0.0;
958
            else
959
#endif
960
1.32M
            {
961
#ifdef REAL_IS_FIXED
962
              gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
963
#endif
964
1.32M
              v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
965
1.32M
            }
966
1.32M
          }
967
22.1M
          mc--;
968
22.1M
        }
969
44.2M
        CHECK_XRPNT;
970
44.2M
        if( (a & (0x8>>i)) )
971
12.6M
        {
972
12.6M
          max = cb;
973
12.6M
          if(part2remain+num <= 0)
974
19.5k
          break;
975
976
12.5M
          if(MSB_MASK) *xrpnt++ = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
977
9.55M
          else         *xrpnt++ =  REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
978
979
12.5M
          num--;
980
12.5M
          mask <<= 1;
981
12.5M
        }
982
31.6M
        else *xrpnt++ = DOUBLE_TO_REAL(0.0);
983
44.2M
      }
984
11.0M
    }
985
986
421k
    gr_info->maxbandl = max+1;
987
421k
    gr_info->maxb = fr->longLimit[sfreq][gr_info->maxbandl];
988
421k
  }
989
990
474k
  part2remain += num;
991
474k
  backbits(fr, num);
992
474k
  num = 0;
993
994
474k
  }
995
293k
  else
996
293k
  {
997
293k
    part2remain = 0;
998
    /* Not entirely sure what good values are, must be > 0. */
999
293k
    gr_info->maxband[0] =
1000
293k
    gr_info->maxband[1] =
1001
293k
    gr_info->maxband[2] =
1002
293k
    gr_info->maxbandl   = 1; /* sfb=maxband[lwin]*3 + lwin - mixed_block_flag must be >= 0 */
1003
293k
    gr_info->maxb       = 1;
1004
293k
  }
1005
1006
272M
  while(xrpnt < xrpntlimit)
1007
272M
  *xrpnt++ = DOUBLE_TO_REAL(0.0);
1008
1009
6.27M
  while( part2remain > 16 )
1010
5.50M
  {
1011
5.50M
    skipbits(fr, 16); /* Dismiss stuffing Bits */
1012
5.50M
    part2remain -= 16;
1013
5.50M
  }
1014
767k
  if(part2remain > 0) skipbits(fr, part2remain);
1015
688k
  else if(part2remain < 0)
1016
156k
  {
1017
156k
    if(VERBOSE2)
1018
156k
      error1("Can't rewind stream by %d bits!",-part2remain);
1019
156k
    return 1; /* -> error */
1020
156k
  }
1021
611k
  return 0;
1022
767k
}
1023
1024
1025
/* calculate real channel values for Joint-I-Stereo-mode */
1026
static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac, struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf)
1027
46.1k
{
1028
46.1k
  real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
1029
46.1k
  const struct bandInfoStruct *bi = &bandInfo[sfreq];
1030
1031
46.1k
  const real *tab1,*tab2;
1032
1033
46.1k
#if 1
1034
46.1k
  int tab;
1035
/* TODO: optimize as static */
1036
46.1k
  const real *tabs[3][2][2] =
1037
46.1k
  { 
1038
46.1k
    { { tan1_1,tan2_1 }       , { tan1_2,tan2_2 } },
1039
46.1k
    { { pow1_1[0],pow2_1[0] } , { pow1_2[0],pow2_2[0] } },
1040
46.1k
    { { pow1_1[1],pow2_1[1] } , { pow1_2[1],pow2_2[1] } }
1041
46.1k
  };
1042
1043
46.1k
  tab = lsf + (gr_info->scalefac_compress & lsf);
1044
46.1k
  tab1 = tabs[tab][ms_stereo][0];
1045
46.1k
  tab2 = tabs[tab][ms_stereo][1];
1046
#else
1047
  if(lsf)
1048
  {
1049
    int p = gr_info->scalefac_compress & 0x1;
1050
    if(ms_stereo)
1051
    {
1052
      tab1 = pow1_2[p];
1053
      tab2 = pow2_2[p];
1054
    }
1055
    else
1056
    {
1057
      tab1 = pow1_1[p];
1058
      tab2 = pow2_1[p];
1059
    }
1060
  }
1061
  else
1062
  {
1063
    if(ms_stereo)
1064
    {
1065
      tab1 = tan1_2;
1066
      tab2 = tan2_2;
1067
    }
1068
    else
1069
    {
1070
      tab1 = tan1_1;
1071
      tab2 = tan2_1;
1072
    }
1073
  }
1074
#endif
1075
1076
46.1k
  if(gr_info->block_type == 2)
1077
13.1k
  {
1078
13.1k
    int lwin,do_l = 0;
1079
13.1k
    if( gr_info->mixed_block_flag ) do_l = 1;
1080
1081
52.7k
    for(lwin=0;lwin<3;lwin++)
1082
39.5k
    { /* process each window */
1083
      /* get first band with zero values */
1084
39.5k
      int is_p,sb,idx,sfb = gr_info->maxband[lwin];  /* sfb is minimal 3 for mixed mode */
1085
39.5k
      if(sfb > 3) do_l = 0;
1086
1087
216k
      for(;sfb<12;sfb++)
1088
177k
      {
1089
177k
        is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ 
1090
177k
        if(is_p != 7)
1091
158k
        {
1092
158k
          real t1,t2;
1093
158k
          sb  = bi->shortDiff[sfb];
1094
158k
          idx = bi->shortIdx[sfb] + lwin;
1095
158k
          t1  = tab1[is_p]; t2 = tab2[is_p];
1096
2.77M
          for (; sb > 0; sb--,idx+=3)
1097
2.61M
          {
1098
2.61M
            real v = xr[0][idx];
1099
2.61M
            xr[0][idx] = REAL_MUL_15(v, t1);
1100
2.61M
            xr[1][idx] = REAL_MUL_15(v, t2);
1101
2.61M
          }
1102
158k
        }
1103
177k
      }
1104
1105
39.5k
#if 1
1106
/* in the original: copy 10 to 11 , here: copy 11 to 12 
1107
maybe still wrong??? (copy 12 to 13?) */
1108
39.5k
      is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1109
39.5k
      sb   = bi->shortDiff[12];
1110
39.5k
      idx  = bi->shortIdx[12] + lwin;
1111
#else
1112
      is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1113
      sb   = bi->shortDiff[11];
1114
      idx  = bi->shortIdx[11] + lwin;
1115
#endif
1116
39.5k
      if(is_p != 7)
1117
35.2k
      {
1118
35.2k
        real t1,t2;
1119
35.2k
        t1 = tab1[is_p]; t2 = tab2[is_p];
1120
966k
        for( ; sb > 0; sb--,idx+=3 )
1121
930k
        {  
1122
930k
          real v = xr[0][idx];
1123
930k
          xr[0][idx] = REAL_MUL_15(v, t1);
1124
930k
          xr[1][idx] = REAL_MUL_15(v, t2);
1125
930k
        }
1126
35.2k
      }
1127
39.5k
    } /* end for(lwin; .. ; . ) */
1128
1129
    /* also check l-part, if ALL bands in the three windows are 'empty' and mode = mixed_mode */
1130
13.1k
    if(do_l)
1131
2.77k
    {
1132
2.77k
      int sfb = gr_info->maxbandl;
1133
2.77k
      int idx;
1134
2.77k
      if(sfb > 21) return; /* similarity fix related to CVE-2006-1655 */
1135
1136
2.77k
      idx = bi->longIdx[sfb];
1137
17.0k
      for( ; sfb<8; sfb++ )
1138
14.3k
      {
1139
14.3k
        int sb = bi->longDiff[sfb];
1140
14.3k
        int is_p = scalefac[sfb]; /* scale: 0-15 */
1141
14.3k
        if(is_p != 7)
1142
13.8k
        {
1143
13.8k
          real t1,t2;
1144
13.8k
          t1 = tab1[is_p]; t2 = tab2[is_p];
1145
100k
          for( ; sb > 0; sb--,idx++)
1146
86.7k
          {
1147
86.7k
            real v = xr[0][idx];
1148
86.7k
            xr[0][idx] = REAL_MUL_15(v, t1);
1149
86.7k
            xr[1][idx] = REAL_MUL_15(v, t2);
1150
86.7k
          }
1151
13.8k
        }
1152
459
        else idx += sb;
1153
14.3k
      }
1154
2.77k
    }     
1155
13.1k
  } 
1156
32.9k
  else
1157
32.9k
  { /* ((gr_info->block_type != 2)) */
1158
32.9k
    int sfb = gr_info->maxbandl;
1159
32.9k
    int is_p,idx;
1160
32.9k
    if(sfb > 21) return; /* tightened fix for CVE-2006-1655 */
1161
1162
31.2k
    idx = bi->longIdx[sfb];
1163
467k
    for ( ; sfb<21; sfb++)
1164
436k
    {
1165
436k
      int sb = bi->longDiff[sfb];
1166
436k
      is_p = scalefac[sfb]; /* scale: 0-15 */
1167
436k
      if(is_p != 7)
1168
429k
      {
1169
429k
        real t1,t2;
1170
429k
        t1 = tab1[is_p]; t2 = tab2[is_p];
1171
11.6M
        for( ; sb > 0; sb--,idx++)
1172
11.1M
        {
1173
11.1M
           real v = xr[0][idx];
1174
11.1M
           xr[0][idx] = REAL_MUL_15(v, t1);
1175
11.1M
           xr[1][idx] = REAL_MUL_15(v, t2);
1176
11.1M
        }
1177
429k
      }
1178
6.30k
      else idx += sb;
1179
436k
    }
1180
1181
31.2k
    is_p = scalefac[20];
1182
31.2k
    if(is_p != 7)
1183
30.4k
    {  /* copy l-band 20 to l-band 21 */
1184
30.4k
      int sb;
1185
30.4k
      real t1 = tab1[is_p],t2 = tab2[is_p]; 
1186
1187
2.81M
      for( sb = bi->longDiff[21]; sb > 0; sb--,idx++ )
1188
2.78M
      {
1189
2.78M
        real v = xr[0][idx];
1190
2.78M
        xr[0][idx] = REAL_MUL_15(v, t1);
1191
2.78M
        xr[1][idx] = REAL_MUL_15(v, t2);
1192
2.78M
      }
1193
30.4k
    }
1194
31.2k
  }
1195
46.1k
}
1196
1197
1198
static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info)
1199
511k
{
1200
511k
  int sblim;
1201
1202
511k
  if(gr_info->block_type == 2)
1203
52.2k
  {
1204
52.2k
      if(!gr_info->mixed_block_flag) return;
1205
1206
27.1k
      sblim = 1; 
1207
27.1k
  }
1208
459k
  else sblim = gr_info->maxb-1;
1209
1210
  /* 31 alias-reduction operations between each pair of sub-bands */
1211
  /* with 8 butterflies between each pair                         */
1212
1213
486k
  {
1214
486k
    int sb;
1215
486k
    real *xr1=(real *) xr[1];
1216
1217
6.22M
    for(sb=sblim; sb; sb--,xr1+=10)
1218
5.73M
    {
1219
5.73M
      int ss;
1220
5.73M
      const real *cs=aa_cs,*ca=aa_ca;
1221
5.73M
      real *xr2 = xr1;
1222
1223
51.6M
      for(ss=7;ss>=0;ss--)
1224
45.9M
      { /* upper and lower butterfly inputs */
1225
45.9M
        register real bu = *--xr2,bd = *xr1;
1226
45.9M
        *xr2   = REAL_MUL(bu, *cs) - REAL_MUL(bd, *ca);
1227
45.9M
        *xr1++ = REAL_MUL(bd, *cs++) + REAL_MUL(bu, *ca++);
1228
45.9M
      }
1229
5.73M
    }
1230
486k
  }
1231
486k
}
1232
1233
/* 
1234
  This is an optimized DCT from Jeff Tsay's maplay 1.2+ package.
1235
  Saved one multiplication by doing the 'twiddle factor' stuff
1236
  together with the window mul. (MH)
1237
1238
  This uses Byeong Gi Lee's Fast Cosine Transform algorithm, but the
1239
  9 point IDCT needs to be reduced further. Unfortunately, I don't
1240
  know how to do that, because 9 is not an even number. - Jeff.
1241
1242
  Original Message:
1243
1244
  9 Point Inverse Discrete Cosine Transform
1245
1246
  This piece of code is Copyright 1997 Mikko Tommila and is freely usable
1247
  by anybody. The algorithm itself is of course in the public domain.
1248
1249
  Again derived heuristically from the 9-point WFTA.
1250
1251
  The algorithm is optimized (?) for speed, not for small rounding errors or
1252
  good readability.
1253
1254
  36 additions, 11 multiplications
1255
1256
  Again this is very likely sub-optimal.
1257
1258
  The code is optimized to use a minimum number of temporary variables,
1259
  so it should compile quite well even on 8-register Intel x86 processors.
1260
  This makes the code quite obfuscated and very difficult to understand.
1261
1262
  References:
1263
  [1] S. Winograd: "On Computing the Discrete Fourier Transform",
1264
      Mathematics of Computation, Volume 32, Number 141, January 1978,
1265
      Pages 175-199
1266
*/
1267
static void INT123_dct36(real *inbuf,real *o1,real *o2,const real *wintab,real *tsbuf)
1268
0
{
1269
0
  real tmp[18];
1270
1271
0
  {
1272
0
    register real *in = inbuf;
1273
1274
0
    in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1275
0
    in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1276
0
    in[11]+=in[10]; in[10]+=in[9];  in[9] +=in[8];
1277
0
    in[8] +=in[7];  in[7] +=in[6];  in[6] +=in[5];
1278
0
    in[5] +=in[4];  in[4] +=in[3];  in[3] +=in[2];
1279
0
    in[2] +=in[1];  in[1] +=in[0];
1280
1281
0
    in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1282
0
    in[9] +=in[7];  in[7] +=in[5];  in[5] +=in[3];  in[3] +=in[1];
1283
1284
0
#if 1
1285
0
    {
1286
0
      real t3;
1287
0
      {
1288
0
        real t0, t1, t2;
1289
1290
0
        t0 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4]));
1291
0
        t1 = REAL_MUL(COS6_2, in[12]);
1292
1293
0
        t3 = in[0];
1294
0
        t2 = t3 - t1 - t1;
1295
0
        tmp[1] = tmp[7] = t2 - t0;
1296
0
        tmp[4]          = t2 + t0 + t0;
1297
0
        t3 += t1;
1298
1299
0
        t2 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2]));
1300
0
        tmp[1] -= t2;
1301
0
        tmp[7] += t2;
1302
0
      }
1303
0
      {
1304
0
        real t0, t1, t2;
1305
1306
0
        t0 = REAL_MUL(cos9[0], (in[4] + in[8] ));
1307
0
        t1 = REAL_MUL(cos9[1], (in[8] - in[16]));
1308
0
        t2 = REAL_MUL(cos9[2], (in[4] + in[16]));
1309
1310
0
        tmp[2] = tmp[6] = t3 - t0      - t2;
1311
0
        tmp[0] = tmp[8] = t3 + t0 + t1;
1312
0
        tmp[3] = tmp[5] = t3      - t1 + t2;
1313
0
      }
1314
0
    }
1315
0
    {
1316
0
      real t1, t2, t3;
1317
1318
0
      t1 = REAL_MUL(cos18[0], (in[2]  + in[10]));
1319
0
      t2 = REAL_MUL(cos18[1], (in[10] - in[14]));
1320
0
      t3 = REAL_MUL(COS6_1,    in[6]);
1321
1322
0
      {
1323
0
        real t0 = t1 + t2 + t3;
1324
0
        tmp[0] += t0;
1325
0
        tmp[8] -= t0;
1326
0
      }
1327
1328
0
      t2 -= t3;
1329
0
      t1 -= t3;
1330
1331
0
      t3 = REAL_MUL(cos18[2], (in[2] + in[14]));
1332
1333
0
      t1 += t3;
1334
0
      tmp[3] += t1;
1335
0
      tmp[5] -= t1;
1336
1337
0
      t2 -= t3;
1338
0
      tmp[2] += t2;
1339
0
      tmp[6] -= t2;
1340
0
    }
1341
1342
#else
1343
    {
1344
      real t0, t1, t2, t3, t4, t5, t6, t7;
1345
1346
      t1 = REAL_MUL(COS6_2, in[12]);
1347
      t2 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4]));
1348
1349
      t3 = in[0] + t1;
1350
      t4 = in[0] - t1 - t1;
1351
      t5     = t4 - t2;
1352
      tmp[4] = t4 + t2 + t2;
1353
1354
      t0 = REAL_MUL(cos9[0], (in[4] + in[8]));
1355
      t1 = REAL_MUL(cos9[1], (in[8] - in[16]));
1356
1357
      t2 = REAL_MUL(cos9[2], (in[4] + in[16]));
1358
1359
      t6 = t3 - t0 - t2;
1360
      t0 += t3 + t1;
1361
      t3 += t2 - t1;
1362
1363
      t2 = REAL_MUL(cos18[0], (in[2]  + in[10]));
1364
      t4 = REAL_MUL(cos18[1], (in[10] - in[14]));
1365
      t7 = REAL_MUL(COS6_1, in[6]);
1366
1367
      t1 = t2 + t4 + t7;
1368
      tmp[0] = t0 + t1;
1369
      tmp[8] = t0 - t1;
1370
      t1 = REAL_MUL(cos18[2], (in[2] + in[14]));
1371
      t2 += t1 - t7;
1372
1373
      tmp[3] = t3 + t2;
1374
      t0 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2]));
1375
      tmp[5] = t3 - t2;
1376
1377
      t4 -= t1 + t7;
1378
1379
      tmp[1] = t5 - t0;
1380
      tmp[7] = t5 + t0;
1381
      tmp[2] = t6 + t4;
1382
      tmp[6] = t6 - t4;
1383
    }
1384
#endif
1385
1386
0
    {
1387
0
      real t0, t1, t2, t3, t4, t5, t6, t7;
1388
1389
0
      t1 = REAL_MUL(COS6_2, in[13]);
1390
0
      t2 = REAL_MUL(COS6_2, (in[9] + in[17] - in[5]));
1391
1392
0
      t3 = in[1] + t1;
1393
0
      t4 = in[1] - t1 - t1;
1394
0
      t5 = t4 - t2;
1395
1396
0
      t0 = REAL_MUL(cos9[0], (in[5] + in[9]));
1397
0
      t1 = REAL_MUL(cos9[1], (in[9] - in[17]));
1398
1399
0
      tmp[13] = REAL_MUL((t4 + t2 + t2), INT123_tfcos36[17-13]);
1400
0
      t2 = REAL_MUL(cos9[2], (in[5] + in[17]));
1401
1402
0
      t6 = t3 - t0 - t2;
1403
0
      t0 += t3 + t1;
1404
0
      t3 += t2 - t1;
1405
1406
0
      t2 = REAL_MUL(cos18[0], (in[3]  + in[11]));
1407
0
      t4 = REAL_MUL(cos18[1], (in[11] - in[15]));
1408
0
      t7 = REAL_MUL(COS6_1, in[7]);
1409
1410
0
      t1 = t2 + t4 + t7;
1411
0
      tmp[17] = REAL_MUL((t0 + t1), INT123_tfcos36[17-17]);
1412
0
      tmp[9]  = REAL_MUL((t0 - t1), INT123_tfcos36[17-9]);
1413
0
      t1 = REAL_MUL(cos18[2], (in[3] + in[15]));
1414
0
      t2 += t1 - t7;
1415
1416
0
      tmp[14] = REAL_MUL((t3 + t2), INT123_tfcos36[17-14]);
1417
0
      t0 = REAL_MUL(COS6_1, (in[11] + in[15] - in[3]));
1418
0
      tmp[12] = REAL_MUL((t3 - t2), INT123_tfcos36[17-12]);
1419
1420
0
      t4 -= t1 + t7;
1421
1422
0
      tmp[16] = REAL_MUL((t5 - t0), INT123_tfcos36[17-16]);
1423
0
      tmp[10] = REAL_MUL((t5 + t0), INT123_tfcos36[17-10]);
1424
0
      tmp[15] = REAL_MUL((t6 + t4), INT123_tfcos36[17-15]);
1425
0
      tmp[11] = REAL_MUL((t6 - t4), INT123_tfcos36[17-11]);
1426
0
    }
1427
1428
0
#define MACRO(v) { \
1429
0
    real tmpval; \
1430
0
    tmpval = tmp[(v)] + tmp[17-(v)]; \
1431
0
    out2[9+(v)] = REAL_MUL(tmpval, w[27+(v)]); \
1432
0
    out2[8-(v)] = REAL_MUL(tmpval, w[26-(v)]); \
1433
0
    tmpval = tmp[(v)] - tmp[17-(v)]; \
1434
0
    ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(tmpval, w[8-(v)]); \
1435
0
    ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(tmpval, w[9+(v)]); }
1436
1437
0
    {
1438
0
      register real *out2 = o2;
1439
0
      register const real *w = wintab;
1440
0
      register real *out1 = o1;
1441
0
      register real *ts = tsbuf;
1442
1443
0
      MACRO(0);
1444
0
      MACRO(1);
1445
0
      MACRO(2);
1446
0
      MACRO(3);
1447
0
      MACRO(4);
1448
0
      MACRO(5);
1449
0
      MACRO(6);
1450
0
      MACRO(7);
1451
0
      MACRO(8);
1452
0
    }
1453
1454
0
  }
1455
0
}
1456
1457
// Wrap the assembly routine calls into C functions that serve as jump target to satisfy
1458
// indirect branch protection if the toolchain enables that. Otherwise, we'd need to anticipate
1459
// that in the assembly (and ensure assemblers support endbr64 and friends).
1460
// Loss of efficiency:
1461
1462
// In the case of one static optimization choice, we do not have that problem.
1463
1464
#ifdef OPT_THE_DCT36
1465
1466
#define DCT36_WRAP(asmfunc) \
1467
6.48M
static void asmfunc ## _wrap(real *inbuf,real *o1,real *o2,const real *wintab,real *tsbuf) \
1468
6.48M
{ \
1469
6.48M
  asmfunc(inbuf, o1, o2, wintab, tsbuf); \
1470
6.48M
}
layer3.c:INT123_dct36_avx_wrap
Line
Count
Source
1467
6.48M
static void asmfunc ## _wrap(real *inbuf,real *o1,real *o2,const real *wintab,real *tsbuf) \
1468
6.48M
{ \
1469
6.48M
  asmfunc(inbuf, o1, o2, wintab, tsbuf); \
1470
6.48M
}
Unexecuted instantiation: layer3.c:INT123_dct36_x86_64_wrap
1471
1472
#ifdef OPT_SSE
1473
DCT36_WRAP(INT123_dct36_sse)
1474
#endif
1475
#ifdef OPT_3DNOWEXT_VINTAGE
1476
DCT36_WRAP(INT123_dct36_3dnowext)
1477
#endif
1478
#ifdef OPT_3DNOW_VINTAGE
1479
DCT36_WRAP(INT123_dct36_3dnow)
1480
#endif
1481
#ifdef OPT_X86_64
1482
DCT36_WRAP(INT123_dct36_x86_64)
1483
#endif
1484
#ifdef OPT_AVX
1485
DCT36_WRAP(INT123_dct36_avx)
1486
#endif
1487
#ifdef OPT_NEON
1488
DCT36_WRAP(INT123_dct36_neon)
1489
#endif
1490
#ifdef OPT_NEON64
1491
DCT36_WRAP(INT123_dct36_neon64)
1492
#endif
1493
1494
int INT123_dct36_match(mpg123_handle *fr, enum optdec t)
1495
0
{
1496
#ifdef OPT_SSE
1497
  if(t == sse && fr->cpu_opts.the_dct36 == INT123_dct36_sse_wrap)
1498
    return 1;
1499
#endif
1500
#ifdef OPT_3DNOWEXT_VINTAGE
1501
  if(t == dreidnowext_vintage && fr->cpu_opts.the_dct36 == INT123_dct36_3dnowext_wrap)
1502
    return 1;
1503
#endif
1504
#ifdef OPT_3DNOW_VINTAGE
1505
  if(t == dreidnow_vintage && fr->cpu_opts.the_dct36 == INT123_dct36_3dnow_wrap)
1506
    return 1;
1507
#endif
1508
0
  return 0;
1509
0
}
1510
1511
void INT123_dct36_choose(mpg123_handle *fr)
1512
305k
{
1513
305k
  switch(fr->cpu_opts.type)
1514
305k
  {
1515
#ifdef OPT_SSE
1516
  case sse:
1517
    fr->cpu_opts.the_dct36 = INT123_dct36_sse_wrap;
1518
  break;
1519
#endif
1520
#ifdef OPT_3DNOWEXT_VINTAGE
1521
  case dreidnowext_vintage:
1522
    fr->cpu_opts.the_dct36 = INT123_dct36_3dnowext_wrap;
1523
  break;
1524
#endif
1525
#ifdef OPT_3DNOW_VINTAGE
1526
  case dreidnow_vintage:
1527
    fr->cpu_opts.the_dct36 = INT123_dct36_3dnow_wrap;
1528
  break;
1529
#endif
1530
0
#ifdef OPT_AVX
1531
305k
  case avx:
1532
305k
    fr->cpu_opts.the_dct36 = INT123_dct36_avx_wrap;
1533
305k
  break;
1534
0
#endif
1535
0
#ifdef OPT_X86_64
1536
0
  case x86_64:
1537
0
    fr->cpu_opts.the_dct36 = INT123_dct36_x86_64_wrap;
1538
0
  break;
1539
0
#endif
1540
#ifdef OPT_NEON
1541
  case neon:
1542
    fr->cpu_opts.the_dct36 = INT123_dct36_neon_wrap;
1543
  break;
1544
#endif
1545
#ifdef OPT_NEON64
1546
  case neon:
1547
    fr->cpu_opts.the_dct36 = INT123_dct36_neon64_wrap;
1548
  break;
1549
#endif
1550
0
  default:
1551
0
    fr->cpu_opts.the_dct36 = INT123_dct36;
1552
305k
  }
1553
305k
}
1554
1555
#endif
1556
1557
/* new DCT12 */
1558
static void dct12(real *in,real *rawout1,real *rawout2,register const real *wi,register real *ts)
1559
627k
{
1560
627k
#define DCT12_PART1 \
1561
1.88M
  in5 = in[5*3];  \
1562
1.88M
  in5 += (in4 = in[4*3]); \
1563
1.88M
  in4 += (in3 = in[3*3]); \
1564
1.88M
  in3 += (in2 = in[2*3]); \
1565
1.88M
  in2 += (in1 = in[1*3]); \
1566
1.88M
  in1 += (in0 = in[0*3]); \
1567
1.88M
  \
1568
1.88M
  in5 += in3; in3 += in1; \
1569
1.88M
  \
1570
1.88M
  in2 = REAL_MUL(in2, COS6_1); \
1571
1.88M
  in3 = REAL_MUL(in3, COS6_1);
1572
1573
627k
#define DCT12_PART2 \
1574
1.88M
  in0 += REAL_MUL(in4, COS6_2); \
1575
1.88M
  \
1576
1.88M
  in4 = in0 + in2; \
1577
1.88M
  in0 -= in2;      \
1578
1.88M
  \
1579
1.88M
  in1 += REAL_MUL(in5, COS6_2); \
1580
1.88M
  \
1581
1.88M
  in5 = REAL_MUL((in1 + in3), tfcos12[0]); \
1582
1.88M
  in1 = REAL_MUL((in1 - in3), tfcos12[2]); \
1583
1.88M
  \
1584
1.88M
  in3 = in4 + in5; \
1585
1.88M
  in4 -= in5;      \
1586
1.88M
  \
1587
1.88M
  in2 = in0 + in1; \
1588
1.88M
  in0 -= in1;
1589
1590
627k
  {
1591
627k
    real in0,in1,in2,in3,in4,in5;
1592
627k
    register real *out1 = rawout1;
1593
627k
    ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1594
627k
    ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1595
 
1596
627k
    DCT12_PART1
1597
1598
627k
    {
1599
627k
      real tmp0,tmp1 = (in0 - in4);
1600
627k
      {
1601
627k
        real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
1602
627k
        tmp0 = tmp1 + tmp2;
1603
627k
        tmp1 -= tmp2;
1604
627k
      }
1605
627k
      ts[(17-1)*SBLIMIT] = out1[17-1] + REAL_MUL(tmp0, wi[11-1]);
1606
627k
      ts[(12+1)*SBLIMIT] = out1[12+1] + REAL_MUL(tmp0, wi[6+1]);
1607
627k
      ts[(6 +1)*SBLIMIT] = out1[6 +1] + REAL_MUL(tmp1, wi[1]);
1608
627k
      ts[(11-1)*SBLIMIT] = out1[11-1] + REAL_MUL(tmp1, wi[5-1]);
1609
627k
    }
1610
1611
627k
    DCT12_PART2
1612
1613
627k
    ts[(17-0)*SBLIMIT] = out1[17-0] + REAL_MUL(in2, wi[11-0]);
1614
627k
    ts[(12+0)*SBLIMIT] = out1[12+0] + REAL_MUL(in2, wi[6+0]);
1615
627k
    ts[(12+2)*SBLIMIT] = out1[12+2] + REAL_MUL(in3, wi[6+2]);
1616
627k
    ts[(17-2)*SBLIMIT] = out1[17-2] + REAL_MUL(in3, wi[11-2]);
1617
1618
627k
    ts[(6 +0)*SBLIMIT]  = out1[6+0] + REAL_MUL(in0, wi[0]);
1619
627k
    ts[(11-0)*SBLIMIT] = out1[11-0] + REAL_MUL(in0, wi[5-0]);
1620
627k
    ts[(6 +2)*SBLIMIT]  = out1[6+2] + REAL_MUL(in4, wi[2]);
1621
627k
    ts[(11-2)*SBLIMIT] = out1[11-2] + REAL_MUL(in4, wi[5-2]);
1622
627k
  }
1623
1624
627k
  in++;
1625
1626
627k
  {
1627
627k
    real in0,in1,in2,in3,in4,in5;
1628
627k
    register real *out2 = rawout2;
1629
 
1630
627k
    DCT12_PART1
1631
1632
627k
    {
1633
627k
      real tmp0,tmp1 = (in0 - in4);
1634
627k
      {
1635
627k
        real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
1636
627k
        tmp0 = tmp1 + tmp2;
1637
627k
        tmp1 -= tmp2;
1638
627k
      }
1639
627k
      out2[5-1] = REAL_MUL(tmp0, wi[11-1]);
1640
627k
      out2[0+1] = REAL_MUL(tmp0, wi[6+1]);
1641
627k
      ts[(12+1)*SBLIMIT] += REAL_MUL(tmp1, wi[1]);
1642
627k
      ts[(17-1)*SBLIMIT] += REAL_MUL(tmp1, wi[5-1]);
1643
627k
    }
1644
1645
627k
    DCT12_PART2
1646
1647
627k
    out2[5-0] = REAL_MUL(in2, wi[11-0]);
1648
627k
    out2[0+0] = REAL_MUL(in2, wi[6+0]);
1649
627k
    out2[0+2] = REAL_MUL(in3, wi[6+2]);
1650
627k
    out2[5-2] = REAL_MUL(in3, wi[11-2]);
1651
1652
627k
    ts[(12+0)*SBLIMIT] += REAL_MUL(in0, wi[0]);
1653
627k
    ts[(17-0)*SBLIMIT] += REAL_MUL(in0, wi[5-0]);
1654
627k
    ts[(12+2)*SBLIMIT] += REAL_MUL(in4, wi[2]);
1655
627k
    ts[(17-2)*SBLIMIT] += REAL_MUL(in4, wi[5-2]);
1656
627k
  }
1657
1658
627k
  in++; 
1659
1660
627k
  {
1661
627k
    real in0,in1,in2,in3,in4,in5;
1662
627k
    register real *out2 = rawout2;
1663
627k
    out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1664
1665
627k
    DCT12_PART1
1666
1667
627k
    {
1668
627k
      real tmp0,tmp1 = (in0 - in4);
1669
627k
      {
1670
627k
        real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
1671
627k
        tmp0 = tmp1 + tmp2;
1672
627k
        tmp1 -= tmp2;
1673
627k
      }
1674
627k
      out2[11-1] = REAL_MUL(tmp0, wi[11-1]);
1675
627k
      out2[6 +1] = REAL_MUL(tmp0, wi[6+1]);
1676
627k
      out2[0+1] += REAL_MUL(tmp1, wi[1]);
1677
627k
      out2[5-1] += REAL_MUL(tmp1, wi[5-1]);
1678
627k
    }
1679
1680
627k
    DCT12_PART2
1681
1682
627k
    out2[11-0] = REAL_MUL(in2, wi[11-0]);
1683
627k
    out2[6 +0] = REAL_MUL(in2, wi[6+0]);
1684
627k
    out2[6 +2] = REAL_MUL(in3, wi[6+2]);
1685
627k
    out2[11-2] = REAL_MUL(in3, wi[11-2]);
1686
1687
627k
    out2[0+0] += REAL_MUL(in0, wi[0]);
1688
627k
    out2[5-0] += REAL_MUL(in0, wi[5-0]);
1689
627k
    out2[0+2] += REAL_MUL(in4, wi[2]);
1690
627k
    out2[5-2] += REAL_MUL(in4, wi[5-2]);
1691
627k
  }
1692
627k
}
1693
1694
1695
static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT], real tsOut[SSLIMIT][SBLIMIT], int ch,struct gr_info_s *gr_info, mpg123_handle *fr)
1696
511k
{
1697
511k
  real (*block)[2][SBLIMIT*SSLIMIT] = fr->hybrid_block;
1698
511k
  int *blc = fr->hybrid_blc;
1699
1700
511k
  real *tspnt = (real *) tsOut;
1701
511k
  real *rawout1,*rawout2;
1702
511k
  int bt = 0;
1703
511k
  size_t sb = 0;
1704
1705
511k
  {
1706
511k
    int b = blc[ch];
1707
511k
    rawout1=block[b][ch];
1708
511k
    b=-b+1;
1709
511k
    rawout2=block[b][ch];
1710
511k
    blc[ch] = b;
1711
511k
  }
1712
  
1713
511k
  if(gr_info->mixed_block_flag)
1714
45.1k
  {
1715
45.1k
    sb = 2;
1716
45.1k
    opt_dct36(fr)(fsIn[0],rawout1,rawout2,win[0],tspnt);
1717
45.1k
    opt_dct36(fr)(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1718
45.1k
    rawout1 += 36; rawout2 += 36; tspnt += 2;
1719
45.1k
  }
1720
 
1721
511k
  bt = gr_info->block_type;
1722
511k
  if(bt == 2)
1723
52.2k
  {
1724
365k
    for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36)
1725
313k
    {
1726
313k
      dct12(fsIn[sb]  ,rawout1   ,rawout2   ,win[2] ,tspnt);
1727
313k
      dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
1728
313k
    }
1729
52.2k
  }
1730
459k
  else
1731
459k
  {
1732
3.65M
    for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36)
1733
3.19M
    {
1734
3.19M
      opt_dct36(fr)(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1735
3.19M
      opt_dct36(fr)(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1736
3.19M
    }
1737
459k
  }
1738
1739
9.77M
  for(;sb<SBLIMIT;sb++,tspnt++)
1740
9.26M
  {
1741
9.26M
    int i;
1742
176M
    for(i=0;i<SSLIMIT;i++)
1743
166M
    {
1744
166M
      tspnt[i*SBLIMIT] = *rawout1++;
1745
166M
      *rawout2++ = DOUBLE_TO_REAL(0.0);
1746
166M
    }
1747
9.26M
  }
1748
511k
}
1749
1750
#ifndef NO_MOREINFO
1751
static void fill_pinfo_side(mpg123_handle *fr, struct III_sideinfo *si, int gr, int stereo1)
1752
0
{
1753
0
  int   i, sb;
1754
0
  float ifqstep; /* Why not double? */
1755
0
  int ch, ss;;
1756
1757
0
  for(ch = 0; ch < stereo1; ++ch)
1758
0
  {
1759
0
    struct gr_info_s *gr_infos = &(si->ch[ch].gr[gr]);
1760
0
    fr->pinfo->big_values[gr][ch] = gr_infos->big_values;
1761
0
    fr->pinfo->scalefac_scale[gr][ch] = gr_infos->scalefac_scale;
1762
0
    fr->pinfo->mixed[gr][ch] = gr_infos->mixed_block_flag;
1763
0
    fr->pinfo->blocktype[gr][ch] = gr_infos->block_type;
1764
0
    fr->pinfo->mainbits[gr][ch] = gr_infos->part2_3_length;
1765
0
    fr->pinfo->preflag[gr][ch] = gr_infos->preflag;
1766
0
    if(gr == 1)
1767
0
      fr->pinfo->scfsi[ch] = gr_infos->scfsi;
1768
0
  }
1769
1770
0
  for(ch = 0; ch < stereo1; ++ch)
1771
0
  {
1772
0
    struct gr_info_s *gr_infos = &(si->ch[ch].gr[gr]);
1773
0
    ifqstep = (fr->pinfo->scalefac_scale[gr][ch] == 0) ? .5 : 1.0;
1774
0
    if(2 == gr_infos->block_type)
1775
0
    {
1776
0
      for(i = 0; i < 3; ++i)
1777
0
      {
1778
0
        for(sb = 0; sb < 12; ++sb)
1779
0
        {
1780
0
          int   j = 3 * sb + i;
1781
          /*
1782
             is_p = scalefac[sfb*3+lwin-gr_infos->mixed_block_flag]; 
1783
          */
1784
          /* scalefac was copied into pinfo->sfb_s[] before */
1785
0
          fr->pinfo->sfb_s[gr][ch][j] = -ifqstep *
1786
0
            fr->pinfo->sfb_s[gr][ch][j - gr_infos->mixed_block_flag];
1787
0
          fr->pinfo->sfb_s[gr][ch][j] -= 2 *
1788
0
            (fr->pinfo->sub_gain[gr][ch][i]);
1789
0
        }
1790
0
        fr->pinfo->sfb_s[gr][ch][3 * sb + i] =
1791
0
          -2 * (fr->pinfo->sub_gain[gr][ch][i]);
1792
0
      }
1793
0
    } else
1794
0
    {
1795
0
      for(sb = 0; sb < 21; ++sb)
1796
0
      {
1797
        /* scalefac was copied into pinfo->sfb[] before */
1798
0
        fr->pinfo->sfb[gr][ch][sb] = fr->pinfo->sfb_s[gr][ch][sb];
1799
0
        if (gr_infos->preflag)
1800
0
          fr->pinfo->sfb[gr][ch][sb] += pretab_choice[1][sb];
1801
0
        fr->pinfo->sfb[gr][ch][sb] *= -ifqstep;
1802
0
      }
1803
0
      fr->pinfo->sfb[gr][ch][21] = 0;
1804
0
    }
1805
0
  }
1806
1807
1808
0
  for(ch = 0; ch < stereo1; ++ch)
1809
0
  {
1810
0
    int j = 0;
1811
0
    for(sb = 0; sb < SBLIMIT; ++sb)
1812
0
      for (ss = 0; ss < SSLIMIT; ++ss, ++j)
1813
0
        fr->pinfo->xr[gr][ch][j] = fr->layer3.hybrid_in[ch][sb][ss];
1814
0
  }
1815
0
}
1816
#endif
1817
1818
/* And at the end... the main layer3 handler */
1819
int INT123_do_layer3(mpg123_handle *fr)
1820
559k
{
1821
559k
  int gr, ch, ss,clip=0;
1822
559k
  int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1823
559k
  struct III_sideinfo sideinfo;
1824
559k
  int stereo = fr->stereo;
1825
559k
  int single = fr->single;
1826
559k
  int ms_stereo,i_stereo;
1827
559k
  int sfreq = fr->hdr.sampling_frequency;
1828
559k
  int stereo1,granules;
1829
1830
559k
  if(stereo == 1)
1831
279k
  { /* stream is mono */
1832
279k
    stereo1 = 1;
1833
279k
    single = SINGLE_LEFT;
1834
279k
  }
1835
280k
  else if(single != SINGLE_STEREO) /* stream is stereo, but force to mono */
1836
0
  stereo1 = 1;
1837
280k
  else
1838
280k
  stereo1 = 2;
1839
1840
559k
  if(fr->hdr.mode == MPG_MD_JOINT_STEREO)
1841
93.9k
  {
1842
93.9k
    ms_stereo = (fr->hdr.mode_ext & 0x2)>>1;
1843
93.9k
    i_stereo  = fr->hdr.mode_ext & 0x1;
1844
93.9k
  }
1845
465k
  else ms_stereo = i_stereo = 0;
1846
1847
559k
  granules = fr->hdr.lsf ? 1 : 2;
1848
1849
  /* quick hack to keep the music playing */
1850
  /* after having seen this nasty test file... */
1851
559k
  if(III_get_side_info(fr, &sideinfo,stereo,ms_stereo,sfreq,single))
1852
20.2k
  {
1853
20.2k
    if(NOQUIET) error("bad frame - unable to get valid sideinfo");
1854
20.2k
    return clip;
1855
20.2k
  }
1856
1857
539k
  INT123_set_pointer(fr, 1, sideinfo.main_data_begin);
1858
539k
#ifndef NO_MOREINFO
1859
539k
  if(fr->pinfo)
1860
0
  {
1861
0
    fr->pinfo->maindata = sideinfo.main_data_begin;
1862
0
    fr->pinfo->padding  = fr->hdr.padding;
1863
0
  }
1864
539k
#endif
1865
863k
  for(gr=0;gr<granules;gr++)
1866
652k
  {
1867
    /*  hybridIn[2][SBLIMIT][SSLIMIT] */
1868
652k
    real (*hybridIn)[SBLIMIT][SSLIMIT] = fr->layer3.hybrid_in;
1869
    /*  hybridOut[2][SSLIMIT][SBLIMIT] */
1870
652k
    real (*hybridOut)[SSLIMIT][SBLIMIT] = fr->layer3.hybrid_out;
1871
1872
652k
    {
1873
652k
      struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
1874
652k
      long part2bits;
1875
652k
      if(gr_info->part2_3_length > fr->bits_avail)
1876
122k
      {
1877
122k
        if(NOQUIET)
1878
122k
          error2(
1879
122k
            "part2_3_length (%u) too large for available bit count (%li)"
1880
122k
          , gr_info->part2_3_length, fr->bits_avail );
1881
122k
        return clip;
1882
122k
      }
1883
529k
      if(fr->hdr.lsf)
1884
201k
      part2bits = III_get_scale_factors_2(fr, scalefacs[0],gr_info,0);
1885
328k
      else
1886
328k
      part2bits = III_get_scale_factors_1(fr, scalefacs[0],gr_info,0,gr);
1887
1888
529k
      if(part2bits < 0)
1889
10.9k
      {
1890
10.9k
        if(VERBOSE2)
1891
10.9k
          error("not enough bits for scale factors");
1892
10.9k
        return clip;
1893
10.9k
      }
1894
1895
518k
#ifndef NO_MOREINFO
1896
518k
      if(fr->pinfo)
1897
0
      {
1898
0
        int i;
1899
0
        fr->pinfo->sfbits[gr][0] = part2bits;
1900
0
        for(i=0; i<39; ++i)
1901
0
          fr->pinfo->sfb_s[gr][0][i] = scalefacs[0][i];
1902
0
      }
1903
518k
#endif
1904
1905
518k
      if(III_dequantize_sample(fr, hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits))
1906
126k
      {
1907
126k
        if(NOQUIET)
1908
126k
          error("dequantization failed!");
1909
126k
        return clip;
1910
126k
      }
1911
391k
      if(fr->bits_avail < 0)
1912
0
      {
1913
0
        if(NOQUIET)
1914
0
          error("bit deficit after dequant");
1915
0
        return clip;
1916
0
      }
1917
391k
    }
1918
1919
391k
    if(stereo == 2)
1920
255k
    {
1921
255k
      struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
1922
255k
      long part2bits;
1923
255k
      if(fr->hdr.lsf) 
1924
71.0k
      part2bits = III_get_scale_factors_2(fr, scalefacs[1],gr_info,i_stereo);
1925
184k
      else
1926
184k
      part2bits = III_get_scale_factors_1(fr, scalefacs[1],gr_info,1,gr);
1927
1928
255k
      if(part2bits < 0)
1929
6.74k
      {
1930
6.74k
        if(VERBOSE2)
1931
6.74k
          error("not enough bits for scale factors");
1932
6.74k
        return clip;
1933
6.74k
      }
1934
1935
249k
#ifndef NO_MOREINFO
1936
249k
      if(fr->pinfo)
1937
0
      {
1938
0
        int i;
1939
0
        fr->pinfo->sfbits[gr][1] = part2bits;
1940
0
        for(i=0; i<39; ++i)
1941
0
          fr->pinfo->sfb_s[gr][1][i] = scalefacs[1][i];
1942
0
      }
1943
249k
#endif
1944
1945
249k
      if(III_dequantize_sample(fr, hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits))
1946
29.8k
      {
1947
29.8k
        if(NOQUIET)
1948
29.8k
          error("dequantization failed!");
1949
29.8k
        return clip;
1950
29.8k
      }
1951
219k
      if(fr->bits_avail < 0)
1952
31.3k
      {
1953
31.3k
        if(NOQUIET)
1954
31.3k
          error("bit deficit after dequant");
1955
31.3k
        return clip;
1956
31.3k
      }
1957
1958
188k
      if(ms_stereo)
1959
108k
      {
1960
108k
        int i;
1961
108k
        unsigned int maxb = sideinfo.ch[0].gr[gr].maxb;
1962
108k
        if(sideinfo.ch[1].gr[gr].maxb > maxb) maxb = sideinfo.ch[1].gr[gr].maxb;
1963
1964
38.9M
        for(i=0;i<SSLIMIT*(int)maxb;i++)
1965
38.8M
        {
1966
38.8M
          real tmp0 = ((real *)hybridIn[0])[i];
1967
38.8M
          real tmp1 = ((real *)hybridIn[1])[i];
1968
38.8M
          ((real *)hybridIn[0])[i] = tmp0 + tmp1;
1969
38.8M
          ((real *)hybridIn[1])[i] = tmp0 - tmp1;
1970
38.8M
        }
1971
108k
      }
1972
1973
188k
      if(i_stereo) III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->hdr.lsf);
1974
1975
188k
      if(ms_stereo || i_stereo || (single == SINGLE_MIX) )
1976
114k
      {
1977
114k
        if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb) 
1978
10.5k
        sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
1979
103k
        else
1980
103k
        gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
1981
114k
      }
1982
1983
188k
      switch(single)
1984
188k
      {
1985
0
        case SINGLE_MIX:
1986
0
        {
1987
0
          register int i;
1988
0
          register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1989
0
          for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++,in0++)
1990
0
          *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */ 
1991
0
        }
1992
0
        break;
1993
0
        case SINGLE_RIGHT:
1994
0
        {
1995
0
          register int i;
1996
0
          register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1997
0
          for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++)
1998
0
          *in0++ = *in1++;
1999
0
        }
2000
0
        break;
2001
188k
      }
2002
188k
    }
2003
2004
323k
#ifndef NO_MOREINFO
2005
323k
    if(fr->pinfo)
2006
0
      fill_pinfo_side(fr, &sideinfo, gr, stereo1);
2007
323k
#endif
2008
2009
835k
    for(ch=0;ch<stereo1;ch++)
2010
511k
    {
2011
511k
      struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
2012
511k
      III_antialias(hybridIn[ch],gr_info);
2013
511k
      III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info, fr);
2014
511k
    }
2015
2016
#ifdef OPT_I486
2017
    if(single != SINGLE_STEREO || fr->af.encoding != MPG123_ENC_SIGNED_16 || fr->down_sample != 0)
2018
    {
2019
#endif
2020
6.15M
    for(ss=0;ss<SSLIMIT;ss++)
2021
5.82M
    {
2022
5.82M
      if(single != SINGLE_STEREO)
2023
2.44M
      clip += (fr->synth_mono)(hybridOut[0][ss], fr);
2024
3.38M
      else
2025
3.38M
      clip += (fr->synth_stereo)(hybridOut[0][ss], hybridOut[1][ss], fr);
2026
2027
5.82M
    }
2028
#ifdef OPT_I486
2029
    } else
2030
    {
2031
      /* Only stereo, 16 bits benefit from the 486 optimization. */
2032
      ss=0;
2033
      while(ss < SSLIMIT)
2034
      {
2035
        int n;
2036
        n=(fr->buffer.size - fr->buffer.fill) / (2*2*32);
2037
        if(n > (SSLIMIT-ss)) n=SSLIMIT-ss;
2038
2039
        /* Clip counting makes no sense with this function. */
2040
        INT123_absynth_1to1_i486(hybridOut[0][ss], 0, fr, n);
2041
        INT123_absynth_1to1_i486(hybridOut[1][ss], 1, fr, n);
2042
        ss+=n;
2043
        fr->buffer.fill+=(2*2*32)*n;
2044
      }
2045
    }
2046
#endif
2047
323k
  }
2048
  
2049
210k
  return clip;
2050
539k
}