Subversion Repositories Kolibri OS

Rev

Rev 1905 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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