PageRenderTime 125ms CodeModel.GetById 13ms app.highlight 98ms RepoModel.GetById 2ms app.codeStats 0ms

/project/jni/sdl_sound/decoders/mpglib/layer3.c

https://github.com/aichunyu/FFPlayer
C | 2020 lines | 1724 code | 194 blank | 102 comment | 350 complexity | d9f36032345bd7516c9192e814251ffb MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/* 
   2 * Mpeg Layer-3 audio decoder 
   3 * --------------------------
   4 * copyright (c) 1995,1996,1997 by Michael Hipp.
   5 * All rights reserved. See also 'README'
   6 */ 
   7
   8#include <stdio.h>
   9#include <stdlib.h>
  10
  11#include "SDL_sound.h"
  12
  13#define __SDL_SOUND_INTERNAL__
  14#include "SDL_sound_internal.h"
  15
  16#include "mpg123_sdlsound.h"
  17#include "mpglib_sdlsound.h"
  18#include "huffman.h"
  19
  20#define MPEG1
  21
  22static real ispow[8207];
  23static real aa_ca[8],aa_cs[8];
  24static real COS1[12][6];
  25static real win[4][36];
  26static real win1[4][36];
  27static real gainpow2[256+118+4];
  28static real COS9[9];
  29static real COS6_1,COS6_2;
  30static real tfcos36[9];
  31static real tfcos12[3];
  32
  33struct bandInfoStruct {
  34  short longIdx[23];
  35  short longDiff[22];
  36  short shortIdx[14];
  37  short shortDiff[13];
  38};
  39
  40int longLimit[9][23];
  41int shortLimit[9][14];
  42
  43struct bandInfoStruct bandInfo[9] = { 
  44
  45/* MPEG 1.0 */
  46 { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
  47   {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
  48   {0,4*3,8*3,12*3,16*3,22*3,30*3,40*3,52*3,66*3, 84*3,106*3,136*3,192*3},
  49   {4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
  50
  51 { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
  52   {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
  53   {0,4*3,8*3,12*3,16*3,22*3,28*3,38*3,50*3,64*3, 80*3,100*3,126*3,192*3},
  54   {4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
  55
  56 { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
  57   {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
  58   {0,4*3,8*3,12*3,16*3,22*3,30*3,42*3,58*3,78*3,104*3,138*3,180*3,192*3} ,
  59   {4,4,4,4,6,8,12,16,20,26,34,42,12} }  ,
  60
  61/* MPEG 2.0 */
  62 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  63   {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
  64   {0,4*3,8*3,12*3,18*3,24*3,32*3,42*3,56*3,74*3,100*3,132*3,174*3,192*3} ,
  65   {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
  66
  67 { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
  68   {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,52,64,70,76,36 } ,
  69   {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,136*3,180*3,192*3} ,
  70   {4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
  71
  72 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  73   {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
  74   {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,134*3,174*3,192*3},
  75   {4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
  76/* MPEG 2.5 */
  77 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
  78   {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
  79   {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
  80   {4,4,4,6,8,10,12,14,18,24,30,40,18} },
  81 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
  82   {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
  83   {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
  84   {4,4,4,6,8,10,12,14,18,24,30,40,18} },
  85 { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
  86   {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
  87   {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
  88   {8,8,8,12,16,20,24,28,36,2,2,2,26} } ,
  89};
  90
  91static int mapbuf0[9][152];
  92static int mapbuf1[9][156];
  93static int mapbuf2[9][44];
  94static int *map[9][3];
  95static int *mapend[9][3];
  96
  97static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
  98static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
  99
 100static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
 101static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16];
 102
 103/* 
 104 * init tables for layer-3 
 105 */
 106void init_layer3(int down_sample_sblimit)
 107{
 108  int i,j,k,l;
 109
 110  for(i=-256;i<118+4;i++)
 111    gainpow2[i+256] = pow((double)2.0,-0.25 * (double) (i+210) );
 112
 113  for(i=0;i<8207;i++)
 114    ispow[i] = pow((double)i,(double)4.0/3.0);
 115
 116  for (i=0;i<8;i++)
 117  {
 118    static double Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
 119    double sq=sqrt(1.0+Ci[i]*Ci[i]);
 120    aa_cs[i] = 1.0/sq;
 121    aa_ca[i] = Ci[i]/sq;
 122  }
 123
 124  for(i=0;i<18;i++)
 125  {
 126    win[0][i]    = win[1][i]    = 0.5 * sin( M_PI / 72.0 * (double) (2*(i+0) +1) ) / cos ( M_PI * (double) (2*(i+0) +19) / 72.0 );
 127    win[0][i+18] = win[3][i+18] = 0.5 * sin( M_PI / 72.0 * (double) (2*(i+18)+1) ) / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 );
 128  }
 129  for(i=0;i<6;i++)
 130  {
 131    win[1][i+18] = 0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 );
 132    win[3][i+12] = 0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 );
 133    win[1][i+24] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+13) ) / cos ( M_PI * (double) (2*(i+24)+19) / 72.0 );
 134    win[1][i+30] = win[3][i] = 0.0;
 135    win[3][i+6 ] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) )  / cos ( M_PI * (double) (2*(i+6 )+19) / 72.0 );
 136  }
 137
 138  for(i=0;i<9;i++)
 139    COS9[i] = cos( M_PI / 18.0 * (double) i);
 140
 141  for(i=0;i<9;i++)
 142    tfcos36[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 );
 143  for(i=0;i<3;i++)
 144    tfcos12[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 );
 145
 146  COS6_1 = cos( M_PI / 6.0 * (double) 1);
 147  COS6_2 = cos( M_PI / 6.0 * (double) 2);
 148
 149  for(i=0;i<12;i++)
 150  {
 151    win[2][i]  = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 );
 152    for(j=0;j<6;j++)
 153      COS1[i][j] = cos( M_PI / 24.0 * (double) ((2*i+7)*(2*j+1)) );
 154  }
 155
 156  for(j=0;j<4;j++) {
 157    static int len[4] = { 36,36,12,36 };
 158    for(i=0;i<len[j];i+=2)
 159      win1[j][i] = + win[j][i];
 160    for(i=1;i<len[j];i+=2)
 161      win1[j][i] = - win[j][i];
 162  }
 163
 164  for(i=0;i<16;i++)
 165  {
 166    double t = tan( (double) i * M_PI / 12.0 );
 167    tan1_1[i] = t / (1.0+t);
 168    tan2_1[i] = 1.0 / (1.0 + t);
 169    tan1_2[i] = M_SQRT2 * t / (1.0+t);
 170    tan2_2[i] = M_SQRT2 / (1.0 + t);
 171
 172    for(j=0;j<2;j++) {
 173      double base = pow(2.0,-0.25*(j+1.0));
 174      double p1=1.0,p2=1.0;
 175      if(i > 0) {
 176        if( i & 1 )
 177          p1 = pow(base,(i+1.0)*0.5);
 178        else
 179          p2 = pow(base,i*0.5);
 180      }
 181      pow1_1[j][i] = p1;
 182      pow2_1[j][i] = p2;
 183      pow1_2[j][i] = M_SQRT2 * p1;
 184      pow2_2[j][i] = M_SQRT2 * p2;
 185    }
 186  }
 187
 188  for(j=0;j<9;j++)
 189  {
 190   struct bandInfoStruct *bi = &bandInfo[j];
 191   int *mp;
 192   int cb,lwin;
 193   short *bdf;
 194
 195   mp = map[j][0] = mapbuf0[j];
 196   bdf = bi->longDiff;
 197   for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) {
 198     *mp++ = (*bdf) >> 1;
 199     *mp++ = i;
 200     *mp++ = 3;
 201     *mp++ = cb;
 202   }
 203   bdf = bi->shortDiff+3;
 204   for(cb=3;cb<13;cb++) {
 205     int l = (*bdf++) >> 1;
 206     for(lwin=0;lwin<3;lwin++) {
 207       *mp++ = l;
 208       *mp++ = i + lwin;
 209       *mp++ = lwin;
 210       *mp++ = cb;
 211     }
 212     i += 6*l;
 213   }
 214   mapend[j][0] = mp;
 215
 216   mp = map[j][1] = mapbuf1[j];
 217   bdf = bi->shortDiff+0;
 218   for(i=0,cb=0;cb<13;cb++) {
 219     int l = (*bdf++) >> 1;
 220     for(lwin=0;lwin<3;lwin++) {
 221       *mp++ = l;
 222       *mp++ = i + lwin;
 223       *mp++ = lwin;
 224       *mp++ = cb;
 225     }
 226     i += 6*l;
 227   }
 228   mapend[j][1] = mp;
 229
 230   mp = map[j][2] = mapbuf2[j];
 231   bdf = bi->longDiff;
 232   for(cb = 0; cb < 22 ; cb++) {
 233     *mp++ = (*bdf++) >> 1;
 234     *mp++ = cb;
 235   }
 236   mapend[j][2] = mp;
 237
 238  }
 239
 240  for(j=0;j<9;j++) {
 241    for(i=0;i<23;i++) {
 242      longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
 243      if(longLimit[j][i] > (down_sample_sblimit) )
 244        longLimit[j][i] = down_sample_sblimit;
 245    }
 246    for(i=0;i<14;i++) {
 247      shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
 248      if(shortLimit[j][i] > (down_sample_sblimit) )
 249        shortLimit[j][i] = down_sample_sblimit;
 250    }
 251  }
 252
 253  for(i=0;i<5;i++) {
 254    for(j=0;j<6;j++) {
 255      for(k=0;k<6;k++) {
 256        int n = k + j * 6 + i * 36;
 257        i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
 258      }
 259    }
 260  }
 261  for(i=0;i<4;i++) {
 262    for(j=0;j<4;j++) {
 263      for(k=0;k<4;k++) {
 264        int n = k + j * 4 + i * 16;
 265        i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
 266      }
 267    }
 268  }
 269  for(i=0;i<4;i++) {
 270    for(j=0;j<3;j++) {
 271      int n = j + i * 3;
 272      i_slen2[n+244] = i|(j<<3) | (5<<12);
 273      n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
 274    }
 275  }
 276
 277  for(i=0;i<5;i++) {
 278    for(j=0;j<5;j++) {
 279      for(k=0;k<4;k++) {
 280        for(l=0;l<4;l++) {
 281          int n = l + k * 4 + j * 16 + i * 80;
 282          n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
 283        }
 284      }
 285    }
 286  }
 287  for(i=0;i<5;i++) {
 288    for(j=0;j<5;j++) {
 289      for(k=0;k<4;k++) {
 290        int n = k + j * 4 + i * 20;
 291        n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
 292      }
 293    }
 294  }
 295}
 296
 297/*
 298 * read additional side information
 299 */
 300#ifdef MPEG1 
 301static int III_get_side_info_1(struct III_sideinfo *si,int stereo,
 302 int ms_stereo,long sfreq,int single)
 303{
 304   int ch, gr;
 305   int powdiff = (single == 3) ? 4 : 0;
 306
 307   si->main_data_begin = getbits(9);
 308   if (stereo == 1)
 309     si->private_bits = getbits_fast(5);
 310   else 
 311     si->private_bits = getbits_fast(3);
 312
 313   for (ch=0; ch<stereo; ch++) {
 314       si->ch[ch].gr[0].scfsi = -1;
 315       si->ch[ch].gr[1].scfsi = getbits_fast(4);
 316   }
 317
 318   for (gr=0; gr<2; gr++) 
 319   {
 320     for (ch=0; ch<stereo; ch++) 
 321     {
 322       register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
 323
 324       gr_info->part2_3_length = getbits(12);
 325       gr_info->big_values = getbits_fast(9);
 326       if(gr_info->big_values > 288) {
 327          SNDDBG(("MPGLIB: big_values too large!\n"));
 328          gr_info->big_values = 288;
 329       }
 330       gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
 331       if(ms_stereo)
 332         gr_info->pow2gain += 2;
 333       gr_info->scalefac_compress = getbits_fast(4);
 334/* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
 335       if(get1bit()) 
 336       {
 337         int i;
 338         gr_info->block_type = getbits_fast(2);
 339         gr_info->mixed_block_flag = get1bit();
 340         gr_info->table_select[0] = getbits_fast(5);
 341         gr_info->table_select[1] = getbits_fast(5);
 342         /*
 343          * table_select[2] not needed, because there is no region2,
 344          * but to satisfy some verifications tools we set it either.
 345          */
 346         gr_info->table_select[2] = 0;
 347         for(i=0;i<3;i++)
 348           gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
 349
 350         if(gr_info->block_type == 0) {
 351           BAIL_MACRO("MPGLIB: Blocktype == 0 and window-switching == 1 not allowed.", 0);
 352         }
 353         /* region_count/start parameters are implicit in this case. */       
 354         gr_info->region1start = 36>>1;
 355         gr_info->region2start = 576>>1;
 356       }
 357       else 
 358       {
 359         int i,r0c,r1c;
 360         for (i=0; i<3; i++)
 361           gr_info->table_select[i] = getbits_fast(5);
 362         r0c = getbits_fast(4);
 363         r1c = getbits_fast(3);
 364         gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
 365         gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
 366         gr_info->block_type = 0;
 367         gr_info->mixed_block_flag = 0;
 368       }
 369       gr_info->preflag = get1bit();
 370       gr_info->scalefac_scale = get1bit();
 371       gr_info->count1table_select = get1bit();
 372     }
 373   }
 374   return !0;
 375}
 376#endif
 377
 378/*
 379 * Side Info for MPEG 2.0 / LSF
 380 */
 381static int III_get_side_info_2(struct III_sideinfo *si,int stereo,
 382 int ms_stereo,long sfreq,int single)
 383{
 384   int ch;
 385   int powdiff = (single == 3) ? 4 : 0;
 386
 387   si->main_data_begin = getbits(8);
 388   if (stereo == 1)
 389     si->private_bits = get1bit();
 390   else 
 391     si->private_bits = getbits_fast(2);
 392
 393   for (ch=0; ch<stereo; ch++) 
 394   {
 395       register struct gr_info_s *gr_info = &(si->ch[ch].gr[0]);
 396
 397       gr_info->part2_3_length = getbits(12);
 398       gr_info->big_values = getbits_fast(9);
 399       if(gr_info->big_values > 288) {
 400         SNDDBG(("MPGLIB: big_values too large!\n"));
 401         gr_info->big_values = 288;
 402       }
 403       gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
 404       if(ms_stereo)
 405         gr_info->pow2gain += 2;
 406       gr_info->scalefac_compress = getbits(9);
 407/* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
 408       if(get1bit()) 
 409       {
 410         int i;
 411         gr_info->block_type = getbits_fast(2);
 412         gr_info->mixed_block_flag = get1bit();
 413         gr_info->table_select[0] = getbits_fast(5);
 414         gr_info->table_select[1] = getbits_fast(5);
 415         /*
 416          * table_select[2] not needed, because there is no region2,
 417          * but to satisfy some verifications tools we set it either.
 418          */
 419         gr_info->table_select[2] = 0;
 420         for(i=0;i<3;i++)
 421           gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
 422
 423         if(gr_info->block_type == 0) {
 424           BAIL_MACRO("MPGLIB: Blocktype == 0 and window-switching == 1 not allowed.", 0);
 425         }
 426         /* region_count/start parameters are implicit in this case. */       
 427/* check this again! */
 428         if(gr_info->block_type == 2)
 429           gr_info->region1start = 36>>1;
 430         else if(sfreq == 8)
 431/* check this for 2.5 and sfreq=8 */
 432           gr_info->region1start = 108>>1;
 433         else
 434           gr_info->region1start = 54>>1;
 435         gr_info->region2start = 576>>1;
 436       }
 437       else 
 438       {
 439         int i,r0c,r1c;
 440         for (i=0; i<3; i++)
 441           gr_info->table_select[i] = getbits_fast(5);
 442         r0c = getbits_fast(4);
 443         r1c = getbits_fast(3);
 444         gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
 445         gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
 446         gr_info->block_type = 0;
 447         gr_info->mixed_block_flag = 0;
 448       }
 449       gr_info->scalefac_scale = get1bit();
 450       gr_info->count1table_select = get1bit();
 451   }
 452   return !0;
 453}
 454
 455/*
 456 * read scalefactors
 457 */
 458#ifdef MPEG1
 459static int III_get_scale_factors_1(int *scf,struct gr_info_s *gr_info)
 460{
 461   static const unsigned char slen[2][16] = {
 462     {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
 463     {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
 464   };
 465   int numbits;
 466   int num0 = slen[0][gr_info->scalefac_compress];
 467   int num1 = slen[1][gr_info->scalefac_compress];
 468
 469    if (gr_info->block_type == 2) {
 470      int i=18;
 471      numbits = (num0 + num1) * 18;
 472
 473      if (gr_info->mixed_block_flag) {
 474         for (i=8;i;i--)
 475           *scf++ = getbits_fast(num0);
 476         i = 9;
 477         numbits -= num0; /* num0 * 17 + num1 * 18 */
 478      }
 479
 480      for (;i;i--)
 481        *scf++ = getbits_fast(num0);
 482      for (i = 18; i; i--)
 483        *scf++ = getbits_fast(num1);
 484      *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
 485    }
 486    else {
 487      int i;
 488      int scfsi = gr_info->scfsi;
 489
 490      if(scfsi < 0) { /* scfsi < 0 => granule == 0 */
 491         for(i=11;i;i--)
 492           *scf++ = getbits_fast(num0);
 493         for(i=10;i;i--)
 494           *scf++ = getbits_fast(num1);
 495         numbits = (num0 + num1) * 10 + num0;
 496         *scf++ = 0;
 497      }
 498      else {
 499        numbits = 0;
 500        if(!(scfsi & 0x8)) {
 501          for (i=0;i<6;i++)
 502            *scf++ = getbits_fast(num0);
 503          numbits += num0 * 6;
 504        }
 505        else {
 506          scf += 6; 
 507        }
 508
 509        if(!(scfsi & 0x4)) {
 510          for (i=0;i<5;i++)
 511            *scf++ = getbits_fast(num0);
 512          numbits += num0 * 5;
 513        }
 514        else {
 515          scf += 5;
 516        }
 517
 518        if(!(scfsi & 0x2)) {
 519          for(i=0;i<5;i++)
 520            *scf++ = getbits_fast(num1);
 521          numbits += num1 * 5;
 522        }
 523        else {
 524          scf += 5; 
 525        }
 526
 527        if(!(scfsi & 0x1)) {
 528          for (i=0;i<5;i++)
 529            *scf++ = getbits_fast(num1);
 530          numbits += num1 * 5;
 531        }
 532        else {
 533           scf += 5;
 534        }
 535        *scf++ = 0;  /* no l[21] in original sources */
 536      }
 537    }
 538    return numbits;
 539}
 540#endif
 541
 542
 543static int III_get_scale_factors_2(int *scf,struct gr_info_s *gr_info,int i_stereo)
 544{
 545  unsigned char *pnt;
 546  int i,j;
 547  unsigned int slen;
 548  int n = 0;
 549  int numbits = 0;
 550
 551  static unsigned char stab[3][6][4] = {
 552   { { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} ,
 553     { 7, 7, 7,0 } , { 6, 6, 6,3 } , {  8, 8,5,0} } ,
 554   { { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} ,
 555     {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } ,
 556   { { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} ,
 557     { 6,15,12,0 } , { 6,12, 9,6 } , {  6,18,9,0} } }; 
 558
 559  if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
 560    slen = i_slen2[gr_info->scalefac_compress>>1];
 561  else
 562    slen = n_slen2[gr_info->scalefac_compress];
 563
 564  gr_info->preflag = (slen>>15) & 0x1;
 565
 566  n = 0;  
 567  if( gr_info->block_type == 2 ) {
 568    n++;
 569    if(gr_info->mixed_block_flag)
 570      n++;
 571  }
 572
 573  pnt = stab[n][(slen>>12)&0x7];
 574
 575  for(i=0;i<4;i++) {
 576    int num = slen & 0x7;
 577    slen >>= 3;
 578    if(num) {
 579      for(j=0;j<(int)(pnt[i]);j++)
 580        *scf++ = getbits_fast(num);
 581      numbits += pnt[i] * num;
 582    }
 583    else {
 584      for(j=0;j<(int)(pnt[i]);j++)
 585        *scf++ = 0;
 586    }
 587  }
 588  
 589  n = (n << 1) + 1;
 590  for(i=0;i<n;i++)
 591    *scf++ = 0;
 592
 593  return numbits;
 594}
 595
 596static int pretab1[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
 597static int pretab2[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 598
 599/*
 600 * don't forget to apply the same changes to III_dequantize_sample_ms() !!! 
 601 */
 602static int III_dequantize_sample(real xr[SBLIMIT][SSLIMIT],int *scf,
 603   struct gr_info_s *gr_info,int sfreq,int part2bits)
 604{
 605  int shift = 1 + gr_info->scalefac_scale;
 606  real *xrpnt = (real *) xr;
 607  int l[3],l3;
 608  int part2remain = gr_info->part2_3_length - part2bits;
 609  int *me;
 610
 611  {
 612    int bv       = gr_info->big_values;
 613    int region1  = gr_info->region1start;
 614    int region2  = gr_info->region2start;
 615
 616    l3 = ((576>>1)-bv)>>1;   
 617/*
 618 * we may lose the 'odd' bit here !! 
 619 * check this later again 
 620 */
 621    if(bv <= region1) {
 622      l[0] = bv; l[1] = 0; l[2] = 0;
 623    }
 624    else {
 625      l[0] = region1;
 626      if(bv <= region2) {
 627        l[1] = bv - l[0];  l[2] = 0;
 628      }
 629      else {
 630        l[1] = region2 - l[0]; l[2] = bv - region2;
 631      }
 632    }
 633  }
 634 
 635  if(gr_info->block_type == 2) {
 636    /*
 637     * decoding with short or mixed mode BandIndex table 
 638     */
 639    int i,max[4];
 640    int step=0,lwin=0,cb=0;
 641    register real v = 0.0;
 642    register int *m,mc;
 643
 644    if(gr_info->mixed_block_flag) {
 645      max[3] = -1;
 646      max[0] = max[1] = max[2] = 2;
 647      m = map[sfreq][0];
 648      me = mapend[sfreq][0];
 649    }
 650    else {
 651      max[0] = max[1] = max[2] = max[3] = -1;
 652      /* max[3] not really needed in this case */
 653      m = map[sfreq][1];
 654      me = mapend[sfreq][1];
 655    }
 656
 657    mc = 0;
 658    for(i=0;i<2;i++) {
 659      int lp = l[i];
 660      struct newhuff *h = ht+gr_info->table_select[i];
 661      for(;lp;lp--,mc--) {
 662        register int x,y;
 663        if( (!mc) ) {
 664          mc = *m++;
 665          xrpnt = ((real *) xr) + (*m++);
 666          lwin = *m++;
 667          cb = *m++;
 668          if(lwin == 3) {
 669            v = gr_info->pow2gain[(*scf++) << shift];
 670            step = 1;
 671          }
 672          else {
 673            v = gr_info->full_gain[lwin][(*scf++) << shift];
 674            step = 3;
 675          }
 676        }
 677        {
 678          register short *val = h->table;
 679          while((y=*val++)<0) {
 680            if (get1bit())
 681              val -= y;
 682            part2remain--;
 683          }
 684          x = y >> 4;
 685          y &= 0xf;
 686        }
 687        if(x == 15) {
 688          max[lwin] = cb;
 689          part2remain -= h->linbits+1;
 690          x += getbits(h->linbits);
 691          if(get1bit())
 692            *xrpnt = -ispow[x] * v;
 693          else
 694            *xrpnt =  ispow[x] * v;
 695        }
 696        else if(x) {
 697          max[lwin] = cb;
 698          if(get1bit())
 699            *xrpnt = -ispow[x] * v;
 700          else
 701            *xrpnt =  ispow[x] * v;
 702          part2remain--;
 703        }
 704        else
 705          *xrpnt = 0.0;
 706        xrpnt += step;
 707        if(y == 15) {
 708          max[lwin] = cb;
 709          part2remain -= h->linbits+1;
 710          y += getbits(h->linbits);
 711          if(get1bit())
 712            *xrpnt = -ispow[y] * v;
 713          else
 714            *xrpnt =  ispow[y] * v;
 715        }
 716        else if(y) {
 717          max[lwin] = cb;
 718          if(get1bit())
 719            *xrpnt = -ispow[y] * v;
 720          else
 721            *xrpnt =  ispow[y] * v;
 722          part2remain--;
 723        }
 724        else
 725          *xrpnt = 0.0;
 726        xrpnt += step;
 727      }
 728    }
 729    for(;l3 && (part2remain > 0);l3--) {
 730      struct newhuff *h = htc+gr_info->count1table_select;
 731      register short *val = h->table,a;
 732
 733      while((a=*val++)<0) {
 734        part2remain--;
 735        if(part2remain < 0) {
 736          part2remain++;
 737          a = 0;
 738          break;
 739        }
 740        if (get1bit())
 741          val -= a;
 742      }
 743
 744      for(i=0;i<4;i++) {
 745        if(!(i & 1)) {
 746          if(!mc) {
 747            mc = *m++;
 748            xrpnt = ((real *) xr) + (*m++);
 749            lwin = *m++;
 750            cb = *m++;
 751            if(lwin == 3) {
 752              v = gr_info->pow2gain[(*scf++) << shift];
 753              step = 1;
 754            }
 755            else {
 756              v = gr_info->full_gain[lwin][(*scf++) << shift];
 757              step = 3;
 758            }
 759          }
 760          mc--;
 761        }
 762        if( (a & (0x8>>i)) ) {
 763          max[lwin] = cb;
 764          part2remain--;
 765          if(part2remain < 0) {
 766            part2remain++;
 767            break;
 768          }
 769          if(get1bit()) 
 770            *xrpnt = -v;
 771          else
 772            *xrpnt = v;
 773        }
 774        else
 775          *xrpnt = 0.0;
 776        xrpnt += step;
 777      }
 778    }
 779 
 780    while( m < me ) {
 781      if(!mc) {
 782        mc = *m++;
 783        xrpnt = ((real *) xr) + *m++;
 784        if( (*m++) == 3)
 785          step = 1;
 786        else
 787          step = 3;
 788        m++; /* cb */
 789      }
 790      mc--;
 791      *xrpnt = 0.0;
 792      xrpnt += step;
 793      *xrpnt = 0.0;
 794      xrpnt += step;
 795/* we could add a little opt. here:
 796 * if we finished a band for window 3 or a long band
 797 * further bands could copied in a simple loop without a
 798 * special 'map' decoding
 799 */
 800    }
 801
 802    gr_info->maxband[0] = max[0]+1;
 803    gr_info->maxband[1] = max[1]+1;
 804    gr_info->maxband[2] = max[2]+1;
 805    gr_info->maxbandl = max[3]+1;
 806
 807    {
 808      int rmax = max[0] > max[1] ? max[0] : max[1];
 809      rmax = (rmax > max[2] ? rmax : max[2]) + 1;
 810      gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
 811    }
 812
 813  }
 814  else {
 815	/*
 816     * decoding with 'long' BandIndex table (block_type != 2)
 817     */
 818    int *pretab = gr_info->preflag ? pretab1 : pretab2;
 819    int i,max = -1;
 820    int cb = 0;
 821    register int *m = map[sfreq][2];
 822    register real v = 0.0;
 823    register int mc = 0;
 824#if 0
 825    me = mapend[sfreq][2];
 826#endif
 827
 828	/*
 829     * long hash table values
 830     */
 831    for(i=0;i<3;i++) {
 832      int lp = l[i];
 833      struct newhuff *h = ht+gr_info->table_select[i];
 834
 835      for(;lp;lp--,mc--) {
 836        int x,y;
 837
 838        if(!mc) {
 839          mc = *m++;
 840          v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
 841          cb = *m++;
 842        }
 843        {
 844          register short *val = h->table;
 845          while((y=*val++)<0) {
 846            if (get1bit())
 847              val -= y;
 848            part2remain--;
 849          }
 850          x = y >> 4;
 851          y &= 0xf;
 852        }
 853        if (x == 15) {
 854          max = cb;
 855          part2remain -= h->linbits+1;
 856          x += getbits(h->linbits);
 857          if(get1bit())
 858            *xrpnt++ = -ispow[x] * v;
 859          else
 860            *xrpnt++ =  ispow[x] * v;
 861        }
 862        else if(x) {
 863          max = cb;
 864          if(get1bit())
 865            *xrpnt++ = -ispow[x] * v;
 866          else
 867            *xrpnt++ =  ispow[x] * v;
 868          part2remain--;
 869        }
 870        else
 871          *xrpnt++ = 0.0;
 872
 873        if (y == 15) {
 874          max = cb;
 875          part2remain -= h->linbits+1;
 876          y += getbits(h->linbits);
 877          if(get1bit())
 878            *xrpnt++ = -ispow[y] * v;
 879          else
 880            *xrpnt++ =  ispow[y] * v;
 881        }
 882        else if(y) {
 883          max = cb;
 884          if(get1bit())
 885            *xrpnt++ = -ispow[y] * v;
 886          else
 887            *xrpnt++ =  ispow[y] * v;
 888          part2remain--;
 889        }
 890        else
 891          *xrpnt++ = 0.0;
 892      }
 893    }
 894
 895	/*
 896     * short (count1table) values
 897     */
 898    for(;l3 && (part2remain > 0);l3--) {
 899      struct newhuff *h = htc+gr_info->count1table_select;
 900      register short *val = h->table,a;
 901
 902      while((a=*val++)<0) {
 903        part2remain--;
 904        if(part2remain < 0) {
 905          part2remain++;
 906          a = 0;
 907          break;
 908        }
 909        if (get1bit())
 910          val -= a;
 911      }
 912
 913      for(i=0;i<4;i++) {
 914        if(!(i & 1)) {
 915          if(!mc) {
 916            mc = *m++;
 917            cb = *m++;
 918            v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
 919          }
 920          mc--;
 921        }
 922        if ( (a & (0x8>>i)) ) {
 923          max = cb;
 924          part2remain--;
 925          if(part2remain < 0) {
 926            part2remain++;
 927            break;
 928          }
 929          if(get1bit())
 930            *xrpnt++ = -v;
 931          else
 932            *xrpnt++ = v;
 933        }
 934        else
 935          *xrpnt++ = 0.0;
 936      }
 937    }
 938
 939	/* 
 940     * zero part
 941     */
 942    for(i=(&xr[SBLIMIT][0]-xrpnt)>>1;i;i--) {
 943      *xrpnt++ = 0.0;
 944      *xrpnt++ = 0.0;
 945    }
 946
 947    gr_info->maxbandl = max+1;
 948    gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
 949  }
 950
 951  while( part2remain > 16 ) {
 952    getbits(16); /* Dismiss stuffing Bits */
 953    part2remain -= 16;
 954  }
 955  if(part2remain > 0)
 956    getbits(part2remain);
 957  else if(part2remain < 0) {
 958    char err[128];
 959    snprintf(err, sizeof (err),
 960                "MPGLIB: Can't rewind stream by %d bits!",
 961                -part2remain);
 962    BAIL_MACRO(err, 1); /* -> error */
 963  }
 964  return 0;
 965}
 966
 967#if 0
 968static int III_dequantize_sample_ms(real xr[2][SBLIMIT][SSLIMIT],int *scf,
 969   struct gr_info_s *gr_info,int sfreq,int part2bits)
 970{
 971  int shift = 1 + gr_info->scalefac_scale;
 972  real *xrpnt = (real *) xr[1];
 973  real *xr0pnt = (real *) xr[0];
 974  int l[3],l3;
 975  int part2remain = gr_info->part2_3_length - part2bits;
 976  int *me;
 977
 978  {
 979    int bv       = gr_info->big_values;
 980    int region1  = gr_info->region1start;
 981    int region2  = gr_info->region2start;
 982
 983    l3 = ((576>>1)-bv)>>1;   
 984/*
 985 * we may lose the 'odd' bit here !! 
 986 * check this later gain 
 987 */
 988    if(bv <= region1) {
 989      l[0] = bv; l[1] = 0; l[2] = 0;
 990    }
 991    else {
 992      l[0] = region1;
 993      if(bv <= region2) {
 994        l[1] = bv - l[0];  l[2] = 0;
 995      }
 996      else {
 997        l[1] = region2 - l[0]; l[2] = bv - region2;
 998      }
 999    }
1000  }
1001 
1002  if(gr_info->block_type == 2) {
1003    int i,max[4];
1004    int step=0,lwin=0,cb=0;
1005    register real v = 0.0;
1006    register int *m,mc = 0;
1007
1008    if(gr_info->mixed_block_flag) {
1009      max[3] = -1;
1010      max[0] = max[1] = max[2] = 2;
1011      m = map[sfreq][0];
1012      me = mapend[sfreq][0];
1013    }
1014    else {
1015      max[0] = max[1] = max[2] = max[3] = -1;
1016      /* max[3] not really needed in this case */
1017      m = map[sfreq][1];
1018      me = mapend[sfreq][1];
1019    }
1020
1021    for(i=0;i<2;i++) {
1022      int lp = l[i];
1023      struct newhuff *h = ht+gr_info->table_select[i];
1024      for(;lp;lp--,mc--) {
1025        int x,y;
1026
1027        if(!mc) {
1028          mc = *m++;
1029          xrpnt = ((real *) xr[1]) + *m;
1030          xr0pnt = ((real *) xr[0]) + *m++;
1031          lwin = *m++;
1032          cb = *m++;
1033          if(lwin == 3) {
1034            v = gr_info->pow2gain[(*scf++) << shift];
1035            step = 1;
1036          }
1037          else {
1038            v = gr_info->full_gain[lwin][(*scf++) << shift];
1039            step = 3;
1040          }
1041        }
1042        {
1043          register short *val = h->table;
1044          while((y=*val++)<0) {
1045            if (get1bit())
1046              val -= y;
1047            part2remain--;
1048          }
1049          x = y >> 4;
1050          y &= 0xf;
1051        }
1052        if(x == 15) {
1053          max[lwin] = cb;
1054          part2remain -= h->linbits+1;
1055          x += getbits(h->linbits);
1056          if(get1bit()) {
1057            real a = ispow[x] * v;
1058            *xrpnt = *xr0pnt + a;
1059            *xr0pnt -= a;
1060          }
1061          else {
1062            real a = ispow[x] * v;
1063            *xrpnt = *xr0pnt - a;
1064            *xr0pnt += a;
1065          }
1066        }
1067        else if(x) {
1068          max[lwin] = cb;
1069          if(get1bit()) {
1070            real a = ispow[x] * v;
1071            *xrpnt = *xr0pnt + a;
1072            *xr0pnt -= a;
1073          }
1074          else {
1075            real a = ispow[x] * v;
1076            *xrpnt = *xr0pnt - a;
1077            *xr0pnt += a;
1078          }
1079          part2remain--;
1080        }
1081        else
1082          *xrpnt = *xr0pnt;
1083        xrpnt += step;
1084        xr0pnt += step;
1085
1086        if(y == 15) {
1087          max[lwin] = cb;
1088          part2remain -= h->linbits+1;
1089          y += getbits(h->linbits);
1090          if(get1bit()) {
1091            real a = ispow[y] * v;
1092            *xrpnt = *xr0pnt + a;
1093            *xr0pnt -= a;
1094          }
1095          else {
1096            real a = ispow[y] * v;
1097            *xrpnt = *xr0pnt - a;
1098            *xr0pnt += a;
1099          }
1100        }
1101        else if(y) {
1102          max[lwin] = cb;
1103          if(get1bit()) {
1104            real a = ispow[y] * v;
1105            *xrpnt = *xr0pnt + a;
1106            *xr0pnt -= a;
1107          }
1108          else {
1109            real a = ispow[y] * v;
1110            *xrpnt = *xr0pnt - a;
1111            *xr0pnt += a;
1112          }
1113          part2remain--;
1114        }
1115        else
1116          *xrpnt = *xr0pnt;
1117        xrpnt += step;
1118        xr0pnt += step;
1119      }
1120    }
1121
1122    for(;l3 && (part2remain > 0);l3--) {
1123      struct newhuff *h = htc+gr_info->count1table_select;
1124      register short *val = h->table,a;
1125
1126      while((a=*val++)<0) {
1127        part2remain--;
1128        if(part2remain < 0) {
1129          part2remain++;
1130          a = 0;
1131          break;
1132        }
1133        if (get1bit())
1134          val -= a;
1135      }
1136
1137      for(i=0;i<4;i++) {
1138        if(!(i & 1)) {
1139          if(!mc) {
1140            mc = *m++;
1141            xrpnt = ((real *) xr[1]) + *m;
1142            xr0pnt = ((real *) xr[0]) + *m++;
1143            lwin = *m++;
1144            cb = *m++;
1145            if(lwin == 3) {
1146              v = gr_info->pow2gain[(*scf++) << shift];
1147              step = 1;
1148            }
1149            else {
1150              v = gr_info->full_gain[lwin][(*scf++) << shift];
1151              step = 3;
1152            }
1153          }
1154          mc--;
1155        }
1156        if( (a & (0x8>>i)) ) {
1157          max[lwin] = cb;
1158          part2remain--;
1159          if(part2remain < 0) {
1160            part2remain++;
1161            break;
1162          }
1163          if(get1bit()) {
1164            *xrpnt = *xr0pnt + v;
1165            *xr0pnt -= v;
1166          }
1167          else {
1168            *xrpnt = *xr0pnt - v;
1169            *xr0pnt += v;
1170          }
1171        }
1172        else
1173          *xrpnt = *xr0pnt;
1174        xrpnt += step;
1175        xr0pnt += step;
1176      }
1177    }
1178 
1179    while( m < me ) {
1180      if(!mc) {
1181        mc = *m++;
1182        xrpnt = ((real *) xr[1]) + *m;
1183        xr0pnt = ((real *) xr[0]) + *m++;
1184        if(*m++ == 3)
1185          step = 1;
1186        else
1187          step = 3;
1188        m++; /* cb */
1189      }
1190      mc--;
1191      *xrpnt = *xr0pnt;
1192      xrpnt += step;
1193      xr0pnt += step;
1194      *xrpnt = *xr0pnt;
1195      xrpnt += step;
1196      xr0pnt += step;
1197/* we could add a little opt. here:
1198 * if we finished a band for window 3 or a long band
1199 * further bands could copied in a simple loop without a
1200 * special 'map' decoding
1201 */
1202    }
1203
1204    gr_info->maxband[0] = max[0]+1;
1205    gr_info->maxband[1] = max[1]+1;
1206    gr_info->maxband[2] = max[2]+1;
1207    gr_info->maxbandl = max[3]+1;
1208
1209    {
1210      int rmax = max[0] > max[1] ? max[0] : max[1];
1211      rmax = (rmax > max[2] ? rmax : max[2]) + 1;
1212      gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
1213    }
1214  }
1215  else {
1216    int *pretab = gr_info->preflag ? pretab1 : pretab2;
1217    int i,max = -1;
1218    int cb = 0;
1219    register int mc=0,*m = map[sfreq][2];
1220    register real v = 0.0;
1221#if 0
1222    me = mapend[sfreq][2];
1223#endif
1224
1225    for(i=0;i<3;i++) {
1226      int lp = l[i];
1227      struct newhuff *h = ht+gr_info->table_select[i];
1228
1229      for(;lp;lp--,mc--) {
1230        int x,y;
1231        if(!mc) {
1232          mc = *m++;
1233          cb = *m++;
1234          v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
1235        }
1236        {
1237          register short *val = h->table;
1238          while((y=*val++)<0) {
1239            if (get1bit())
1240              val -= y;
1241            part2remain--;
1242          }
1243          x = y >> 4;
1244          y &= 0xf;
1245        }
1246        if (x == 15) {
1247          max = cb;
1248          part2remain -= h->linbits+1;
1249          x += getbits(h->linbits);
1250          if(get1bit()) {
1251            real a = ispow[x] * v;
1252            *xrpnt++ = *xr0pnt + a;
1253            *xr0pnt++ -= a;
1254          }
1255          else {
1256            real a = ispow[x] * v;
1257            *xrpnt++ = *xr0pnt - a;
1258            *xr0pnt++ += a;
1259          }
1260        }
1261        else if(x) {
1262          max = cb;
1263          if(get1bit()) {
1264            real a = ispow[x] * v;
1265            *xrpnt++ = *xr0pnt + a;
1266            *xr0pnt++ -= a;
1267          }
1268          else {
1269            real a = ispow[x] * v;
1270            *xrpnt++ = *xr0pnt - a;
1271            *xr0pnt++ += a;
1272          }
1273          part2remain--;
1274        }
1275        else
1276          *xrpnt++ = *xr0pnt++;
1277
1278        if (y == 15) {
1279          max = cb;
1280          part2remain -= h->linbits+1;
1281          y += getbits(h->linbits);
1282          if(get1bit()) {
1283            real a = ispow[y] * v;
1284            *xrpnt++ = *xr0pnt + a;
1285            *xr0pnt++ -= a;
1286          }
1287          else {
1288            real a = ispow[y] * v;
1289            *xrpnt++ = *xr0pnt - a;
1290            *xr0pnt++ += a;
1291          }
1292        }
1293        else if(y) {
1294          max = cb;
1295          if(get1bit()) {
1296            real a = ispow[y] * v;
1297            *xrpnt++ = *xr0pnt + a;
1298            *xr0pnt++ -= a;
1299          }
1300          else {
1301            real a = ispow[y] * v;
1302            *xrpnt++ = *xr0pnt - a;
1303            *xr0pnt++ += a;
1304          }
1305          part2remain--;
1306        }
1307        else
1308          *xrpnt++ = *xr0pnt++;
1309      }
1310    }
1311
1312    for(;l3 && (part2remain > 0);l3--) {
1313      struct newhuff *h = htc+gr_info->count1table_select;
1314      register short *val = h->table,a;
1315
1316      while((a=*val++)<0) {
1317        part2remain--;
1318        if(part2remain < 0) {
1319          part2remain++;
1320          a = 0;
1321          break;
1322        }
1323        if (get1bit())
1324          val -= a;
1325      }
1326
1327      for(i=0;i<4;i++) {
1328        if(!(i & 1)) {
1329          if(!mc) {
1330            mc = *m++;
1331            cb = *m++;
1332            v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
1333          }
1334          mc--;
1335        }
1336        if ( (a & (0x8>>i)) ) {
1337          max = cb;
1338          part2remain--;
1339          if(part2remain <= 0) {
1340            part2remain++;
1341            break;
1342          }
1343          if(get1bit()) {
1344            *xrpnt++ = *xr0pnt + v;
1345            *xr0pnt++ -= v;
1346          }
1347          else {
1348            *xrpnt++ = *xr0pnt - v;
1349            *xr0pnt++ += v;
1350          }
1351        }
1352        else
1353          *xrpnt++ = *xr0pnt++;
1354      }
1355    }
1356    for(i=(&xr[1][SBLIMIT][0]-xrpnt)>>1;i;i--) {
1357      *xrpnt++ = *xr0pnt++;
1358      *xrpnt++ = *xr0pnt++;
1359    }
1360
1361    gr_info->maxbandl = max+1;
1362    gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
1363  }
1364
1365  while ( part2remain > 16 ) {
1366    getbits(16); /* Dismiss stuffing Bits */
1367    part2remain -= 16;
1368  }
1369  if(part2remain > 0 )
1370    getbits(part2remain);
1371  else if(part2remain < 0) {
1372    char err[128];
1373    snprintf(err, sizeof (err),
1374                "MPGLIB: Can't rewind stream by %d bits!",
1375                -part2remain);
1376    BAIL_MACRO(err, 1); /* -> error */
1377  }
1378  return 0;
1379}
1380#endif
1381
1382/* 
1383 * III_stereo: calculate real channel values for Joint-I-Stereo-mode
1384 */
1385static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac,
1386   struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf)
1387{
1388      real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
1389      struct bandInfoStruct *bi = &bandInfo[sfreq];
1390      real *tab1,*tab2;
1391
1392      if(lsf) {
1393        int p = gr_info->scalefac_compress & 0x1;
1394	    if(ms_stereo) {
1395          tab1 = pow1_2[p]; tab2 = pow2_2[p];
1396        }
1397        else {
1398          tab1 = pow1_1[p]; tab2 = pow2_1[p];
1399        }
1400      }
1401      else {
1402        if(ms_stereo) {
1403          tab1 = tan1_2; tab2 = tan2_2;
1404        }
1405        else {
1406          tab1 = tan1_1; tab2 = tan2_1;
1407        }
1408      }
1409
1410      if (gr_info->block_type == 2)
1411      {
1412         int lwin,do_l = 0;
1413         if( gr_info->mixed_block_flag )
1414           do_l = 1;
1415
1416         for (lwin=0;lwin<3;lwin++) /* process each window */
1417         {
1418             /* get first band with zero values */
1419           int is_p,sb,idx,sfb = gr_info->maxband[lwin];  /* sfb is minimal 3 for mixed mode */
1420           if(sfb > 3)
1421             do_l = 0;
1422
1423           for(;sfb<12;sfb++)
1424           {
1425             is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */ 
1426             if(is_p != 7) {
1427               real t1,t2;
1428               sb = bi->shortDiff[sfb];
1429               idx = bi->shortIdx[sfb] + lwin;
1430               t1 = tab1[is_p]; t2 = tab2[is_p];
1431               for (; sb > 0; sb--,idx+=3)
1432               {
1433                 real v = xr[0][idx];
1434                 xr[0][idx] = v * t1;
1435                 xr[1][idx] = v * t2;
1436               }
1437             }
1438           }
1439
1440#if 1
1441/* in the original: copy 10 to 11 , here: copy 11 to 12 
1442maybe still wrong??? (copy 12 to 13?) */
1443           is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1444           sb = bi->shortDiff[12];
1445           idx = bi->shortIdx[12] + lwin;
1446#else
1447           is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1448           sb = bi->shortDiff[11];
1449           idx = bi->shortIdx[11] + lwin;
1450#endif
1451           if(is_p != 7)
1452           {
1453             real t1,t2;
1454             t1 = tab1[is_p]; t2 = tab2[is_p];
1455             for ( ; sb > 0; sb--,idx+=3 )
1456             {  
1457               real v = xr[0][idx];
1458               xr[0][idx] = v * t1;
1459               xr[1][idx] = v * t2;
1460             }
1461           }
1462         } /* end for(lwin; .. ; . ) */
1463
1464         if (do_l)
1465         {
1466/* also check l-part, if ALL bands in the three windows are 'empty'
1467 * and mode = mixed_mode 
1468 */
1469           int sfb = gr_info->maxbandl;
1470           int idx = bi->longIdx[sfb];
1471
1472           for ( ; sfb<8; sfb++ )
1473           {
1474             int sb = bi->longDiff[sfb];
1475             int is_p = scalefac[sfb]; /* scale: 0-15 */
1476             if(is_p != 7) {
1477               real t1,t2;
1478               t1 = tab1[is_p]; t2 = tab2[is_p];
1479               for ( ; sb > 0; sb--,idx++)
1480               {
1481                 real v = xr[0][idx];
1482                 xr[0][idx] = v * t1;
1483                 xr[1][idx] = v * t2;
1484               }
1485             }
1486             else 
1487               idx += sb;
1488           }
1489         }     
1490      } 
1491      else /* ((gr_info->block_type != 2)) */
1492      {
1493        int sfb = gr_info->maxbandl;
1494        int is_p,idx = bi->longIdx[sfb];
1495        for ( ; sfb<21; sfb++)
1496        {
1497          int sb = bi->longDiff[sfb];
1498          is_p = scalefac[sfb]; /* scale: 0-15 */
1499          if(is_p != 7) {
1500            real t1,t2;
1501            t1 = tab1[is_p]; t2 = tab2[is_p];
1502            for ( ; sb > 0; sb--,idx++)
1503            {
1504               real v = xr[0][idx];
1505               xr[0][idx] = v * t1;
1506               xr[1][idx] = v * t2;
1507            }
1508          }
1509          else
1510            idx += sb;
1511        }
1512
1513        is_p = scalefac[20]; /* copy l-band 20 to l-band 21 */
1514        if(is_p != 7)
1515        {
1516          int sb;
1517          real t1 = tab1[is_p],t2 = tab2[is_p]; 
1518
1519          for ( sb = bi->longDiff[21]; sb > 0; sb--,idx++ )
1520          {
1521            real v = xr[0][idx];
1522            xr[0][idx] = v * t1;
1523            xr[1][idx] = v * t2;
1524          }
1525        }
1526      } /* ... */
1527}
1528
1529static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info)
1530{
1531   int sblim;
1532
1533   if(gr_info->block_type == 2)
1534   {
1535      if(!gr_info->mixed_block_flag) 
1536        return;
1537      sblim = 1; 
1538   }
1539   else {
1540     sblim = gr_info->maxb-1;
1541   }
1542
1543   /* 31 alias-reduction operations between each pair of sub-bands */
1544   /* with 8 butterflies between each pair                         */
1545
1546   {
1547     int sb;
1548     real *xr1=(real *) xr[1];
1549
1550     for(sb=sblim;sb;sb--,xr1+=10)
1551     {
1552       int ss;
1553       real *cs=aa_cs,*ca=aa_ca;
1554       real *xr2 = xr1;
1555
1556       for(ss=7;ss>=0;ss--)
1557       {       /* upper and lower butterfly inputs */
1558         register real bu = *--xr2,bd = *xr1;
1559         *xr2   = (bu * (*cs)   ) - (bd * (*ca)   );
1560         *xr1++ = (bd * (*cs++) ) + (bu * (*ca++) );
1561       }
1562     }
1563  }
1564}
1565
1566/*
1567 DCT insipired by Jeff Tsay's DCT from the maplay package
1568 this is an optimized version with manual unroll.
1569
1570 References:
1571 [1] S. Winograd: "On Computing the Discrete Fourier Transform",
1572     Mathematics of Computation, Volume 32, Number 141, January 1978,
1573     Pages 175-199
1574*/
1575
1576static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1577{
1578  {
1579    register real *in = inbuf;
1580
1581    in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1582    in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1583    in[11]+=in[10]; in[10]+=in[9];  in[9] +=in[8];
1584    in[8] +=in[7];  in[7] +=in[6];  in[6] +=in[5];
1585    in[5] +=in[4];  in[4] +=in[3];  in[3] +=in[2];
1586    in[2] +=in[1];  in[1] +=in[0];
1587
1588    in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1589    in[9] +=in[7];  in[7] +=in[5];  in[5] +=in[3];  in[3] +=in[1];
1590
1591
1592  {
1593
1594#define MACRO0(v) { \
1595    real tmp; \
1596    out2[9+(v)] = (tmp = sum0 + sum1) * w[27+(v)]; \
1597    out2[8-(v)] = tmp * w[26-(v)];  } \
1598    sum0 -= sum1; \
1599    ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \
1600    ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)]; 
1601#define MACRO1(v) { \
1602	real sum0,sum1; \
1603    sum0 = tmp1a + tmp2a; \
1604	sum1 = (tmp1b + tmp2b) * tfcos36[(v)]; \
1605	MACRO0(v); }
1606#define MACRO2(v) { \
1607    real sum0,sum1; \
1608    sum0 = tmp2a - tmp1a; \
1609    sum1 = (tmp2b - tmp1b) * tfcos36[(v)]; \
1610	MACRO0(v); }
1611
1612    register const real *c = COS9;
1613    register real *out2 = o2;
1614	register real *w = wintab;
1615	register real *out1 = o1;
1616	register real *ts = tsbuf;
1617
1618    real ta33,ta66,tb33,tb66;
1619
1620    ta33 = in[2*3+0] * c[3];
1621    ta66 = in[2*6+0] * c[6];
1622    tb33 = in[2*3+1] * c[3];
1623    tb66 = in[2*6+1] * c[6];
1624
1625    { 
1626      real tmp1a,tmp2a,tmp1b,tmp2b;
1627      tmp1a =             in[2*1+0] * c[1] + ta33 + in[2*5+0] * c[5] + in[2*7+0] * c[7];
1628      tmp1b =             in[2*1+1] * c[1] + tb33 + in[2*5+1] * c[5] + in[2*7+1] * c[7];
1629      tmp2a = in[2*0+0] + in[2*2+0] * c[2] + in[2*4+0] * c[4] + ta66 + in[2*8+0] * c[8];
1630      tmp2b = in[2*0+1] + in[2*2+1] * c[2] + in[2*4+1] * c[4] + tb66 + in[2*8+1] * c[8];
1631
1632      MACRO1(0);
1633      MACRO2(8);
1634    }
1635
1636    {
1637      real tmp1a,tmp2a,tmp1b,tmp2b;
1638      tmp1a = ( in[2*1+0] - in[2*5+0] - in[2*7+0] ) * c[3];
1639      tmp1b = ( in[2*1+1] - in[2*5+1] - in[2*7+1] ) * c[3];
1640      tmp2a = ( in[2*2+0] - in[2*4+0] - in[2*8+0] ) * c[6] - in[2*6+0] + in[2*0+0];
1641      tmp2b = ( in[2*2+1] - in[2*4+1] - in[2*8+1] ) * c[6] - in[2*6+1] + in[2*0+1];
1642
1643      MACRO1(1);
1644      MACRO2(7);
1645    }
1646
1647    {
1648      real tmp1a,tmp2a,tmp1b,tmp2b;
1649      tmp1a =             in[2*1+0] * c[5] - ta33 - in[2*5+0] * c[7] + in[2*7+0] * c[1];
1650      tmp1b =             in[2*1+1] * c[5] - tb33 - in[2*5+1] * c[7] + in[2*7+1] * c[1];
1651      tmp2a = in[2*0+0] - in[2*2+0] * c[8] - in[2*4+0] * c[2] + ta66 + in[2*8+0] * c[4];
1652      tmp2b = in[2*0+1] - in[2*2+1] * c[8] - in[2*4+1] * c[2] + tb66 + in[2*8+1] * c[4];
1653
1654      MACRO1(2);
1655      MACRO2(6);
1656    }
1657
1658    {
1659      real tmp1a,tmp2a,tmp1b,tmp2b;
1660      tmp1a =             in[2*1+0] * c[7] - ta33 + in[2*5+0] * c[1] - in[2*7+0] * c[5];
1661      tmp1b =             in[2*1+1] * c[7] - tb33 + in[2*5+1] * c[1] - in[2*7+1] * c[5];
1662      tmp2a = in[2*0+0] - in[2*2+0] * c[4] + in[2*4+0] * c[8] + ta66 - in[2*8+0] * c[2];
1663      tmp2b = in[2*0+1] - in[2*2+1] * c[4] + in[2*4+1] * c[8] + tb66 - in[2*8+1] * c[2];
1664
1665      MACRO1(3);
1666      MACRO2(5);
1667    }
1668
1669	{
1670		real sum0,sum1;
1671    	sum0 =  in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
1672    	sum1 = (in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ) * tfcos36[4];
1673		MACRO0(4);
1674	}
1675  }
1676
1677  }
1678}
1679
1680/*
1681 * new DCT12
1682 */
1683static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts)
1684{
1685#define DCT12_PART1 \
1686             in5 = in[5*3];  \
1687     in5 += (in4 = in[4*3]); \
1688     in4 += (in3 = in[3*3]); \
1689     in3 += (in2 = in[2*3]); \
1690     in2 += (in1 = in[1*3]); \
1691     in1 += (in0 = in[0*3]); \
1692                             \
1693     in5 += in3; in3 += in1; \
1694                             \
1695     in2 *= COS6_1; \
1696     in3 *= COS6_1; \
1697
1698#define DCT12_PART2 \
1699     in0 += in4 * COS6_2; \
1700                          \
1701     in4 = in0 + in2;     \
1702     in0 -= in2;          \
1703                          \
1704     in1 += in5 * COS6_2; \
1705                          \
1706     in5 = (in1 + in3) * tfcos12[0]; \
1707     in1 = (in1 - in3) * tfcos12[2]; \
1708                         \
1709     in3 = in4 + in5;    \
1710     in4 -= in5;         \
1711                         \
1712     in2 = in0 + in1;    \
1713     in0 -= in1;
1714
1715
1716   {
1717     real in0,in1,in2,in3,in4,in5;
1718     register real *out1 = rawout1;
1719     ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1720     ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1721 
1722     DCT12_PART1
1723
1724     {
1725       real tmp0,tmp1 = (in0 - in4);
1726       {
1727         real tmp2 = (in1 - in5) * tfcos12[1];
1728         tmp0 = tmp1 + tmp2;
1729         tmp1 -= tmp2;
1730       }
1731       ts[(17-1)*SBLIMIT] = out1[17-1] + tmp0 * wi[11-1];
1732       ts[(12+1)*SBLIMIT] = out1[12+1] + tmp0 * wi[6+1];
1733       ts[(6 +1)*SBLIMIT] = out1[6 +1] + tmp1 * wi[1];
1734       ts[(11-1)*SBLIMIT] = out1[11-1] + tmp1 * wi[5-1];
1735     }
1736
1737     DCT12_PART2
1738
1739     ts[(17-0)*SBLIMIT] = out1[17-0] + in2 * wi[11-0];
1740     ts[(12+0)*SBLIMIT] = out1[12+0] + in2 * wi[6+0];
1741     ts[(12+2)*SBLIMIT] = out1[12+2] + in3 * wi[6+2];
1742     ts[(17-2)*SBLIMIT] = out1[17-2] + in3 * wi[11-2];
1743
1744     ts[(6+0)*SBLIMIT]  = out1[6+0] + in0 * wi[0];
1745     ts[(11-0)*SBLIMIT] = out1[11-0] + in0 * wi[5-0];
1746     ts[(6+2)*SBLIMIT]  = out1[6+2] + in4 * wi[2];
1747     ts[(11-2)*SBLIMIT] = out1[11-2] + in4 * wi[5-2];
1748  }
1749
1750  in++;
1751
1752  {
1753     real in0,in1,in2,in3,in4,in5;
1754     register real *out2 = rawout2;
1755 
1756     DCT12_PART1
1757
1758     {
1759       real tmp0,tmp1 = (in0 - in4);
1760       {
1761         real tmp2 = (in1 - in5) * tfcos12[1];
1762         tmp0 = tmp1 + tmp2;
1763         tmp1 -= tmp2;
1764       }
1765       out2[5-1] = tmp0 * wi[11-1];
1766       out2[0+1] = tmp0 * wi[6+1];
1767       ts[(12+1)*SBLIMIT] += tmp1 * wi[1];
1768       ts[(17-1)*SBLIMIT] += tmp1 * wi[5-1];
1769     }
1770
1771     DCT12_PART2
1772
1773     out2[5-0] = in2 * wi[11-0];
1774     out2[0+0] = in2 * wi[6+0];
1775     out2[0+2] = in3 * wi[6+2];
1776     out2[5-2] = in3 * wi[11-2];
1777
1778     ts[(12+0)*SBLIMIT] += in0 * wi[0];
1779     ts[(17-0)*SBLIMIT] += in0 * wi[5-0];
1780     ts[(12+2)*SBLIMIT] += in4 * wi[2];
1781     ts[(17-2)*SBLIMIT] += in4 * wi[5-2];
1782  }
1783
1784  in++; 
1785
1786  {
1787     real in0,in1,in2,in3,in4,in5;
1788     register real *out2 = rawout2;
1789     out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1790
1791     DCT12_PART1
1792
1793     {
1794       real tmp0,tmp1 = (in0 - in4);
1795       {
1796         real tmp2 = (in1 - in5) * tfcos12[1];
1797         tmp0 = tmp1 + tmp2;
1798         tmp1 -= tmp2;
1799       }
1800       out2[11-1] = tmp0 * wi[11-1];
1801       out2[6 +1] = tmp0 * wi[6+1];
1802       out2[0+1] += tmp1 * wi[1];
1803       out2[5-1] += tmp1 * wi[5-1];
1804     }
1805
1806     DCT12_PART2
1807
1808     out2[11-0] = in2 * wi[11-0];
1809     out2[6 +0] = in2 * wi[6+0];
1810     out2[6 +2] = in3 * wi[6+2];
1811     out2[11-2] = in3 * wi[11-2];
1812
1813     out2[0+0] += in0 * wi[0];
1814     out2[5-0] += in0 * wi[5-0];
1815     out2[0+2] += in4 * wi[2];
1816     out2[5-2] += in4 * wi[5-2];
1817  }
1818}
1819
1820/*
1821 * III_hybrid
1822 */
1823static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT],
1824   int ch,struct gr_info_s *gr_info,struct mpstr *mp)
1825{
1826   real *tspnt = (real *) tsOut;
1827   real (*block)[2][SBLIMIT*SSLIMIT] = mp->hybrid_block;
1828   int *blc = mp->hybrid_blc;
1829   real *rawout1,*rawout2;
1830   int bt;
1831   int sb = 0;
1832
1833   {
1834     int b = blc[ch];
1835     rawout1=block[b][ch];
1836     b=-b+1;
1837     rawout2=block[b][ch];
1838     blc[ch] = b;
1839   }
1840
1841  
1842   if(gr_info->mixed_block_flag) {
1843     sb = 2;
1844     dct36(fsIn[0],rawout1,rawout2,win[0],tspnt);
1845     dct36(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1846     rawout1 += 36; rawout2 += 36; tspnt += 2;
1847   }
1848 
1849   bt = gr_info->block_type;
1850   if(bt == 2) {
1851     for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1852       dct12(fsIn[sb],rawout1,rawout2,win[2],tspnt);
1853       dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
1854     }
1855   }
1856   else {
1857     for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1858       dct36(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1859       dct36(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1860     }
1861   }
1862
1863   for(;sb<SBLIMIT;sb++,tspnt++) {
1864     int i;
1865     for(i=0;i<SSLIMIT;i++) {
1866       tspnt[i*SBLIMIT] = *rawout1++;
1867       *rawout2++ = 0.0;
1868     }
1869   }
1870}
1871
1872/*
1873 * main layer3 handler
1874 */
1875int do_layer3(struct frame *fr,unsigned char *pcm_sample,
1876              int *pcm_point,struct mpstr *mp)
1877{
1878  int gr, ch, ss,clip=0;
1879  int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1880  struct III_sideinfo sideinfo;
1881  int stereo = fr->stereo;
1882  int single = fr->single;
1883  int ms_stereo,i_stereo;
1884  int sfreq = fr->sampling_frequency;
1885  int stereo1,granules;
1886
1887  if(stereo == 1) { /* stream is mono */
1888    stereo1 = 1;
1889    single = 0;
1890  }
1891  else if(single >= 0) /* stream is stereo, but force to mono */
1892    stereo1 = 1;
1893  else
1894    stereo1 = 2;
1895
1896  if(fr->mode == MPG_MD_JOINT_STEREO) {
1897    ms_stereo = fr->mode_ext & 0x2;
1898    i_stereo  = fr->mode_ext & 0x1;
1899  }
1900  else
1901    ms_stereo = i_stereo = 0;
1902
1903  if(fr->lsf) {
1904    granules = 1;
1905    if(!III_get_side_info_2(&sideinfo,stereo,ms_stereo,sfreq,single))
1906      return -1;
1907  }
1908  else {
1909    granules = 2;
1910#ifdef MPEG1
1911    if(!III_get_side_info_1(&sideinfo,stereo,ms_stereo,sfreq,single))
1912      return -1;
1913#else
1914    __Sound_SetError("MPGLIB: Not supported!");
1915#endif
1916  }
1917
1918  if(set_pointer(sideinfo.main_data_begin,mp) == MP3_ERR)
1919    return -1;
1920
1921  for (gr=0;gr<granules;gr++) 
1922  {
1923    real hybridIn[2][SBLIMIT][SSLIMIT];
1924    real hybridOut[2][SSLIMIT][SBLIMIT];
1925    memset(hybridIn, '\

Large files files are truncated, but you can click here to view the full file