home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 August - Disc 3 / chip_20018103_hu.iso / amiga / chiputil / raplay.lha / RAPlay / src / decode288.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-24  |  4.8 KB  |  233 lines

  1. #include "common.h"
  2. #include "decode288.h"
  3. #include "tables288.h"
  4.  
  5. /* Initialize internal variable structure */
  6. Real_288 *init_288(void)
  7. {
  8.     Real_internal *glob;
  9.  
  10.     if ((glob=malloc(sizeof(Real_internal))))
  11.       memset(glob,0,sizeof(Real_internal));
  12.  
  13.     return (Real_288 *)glob;
  14. }
  15.  
  16. /* Free internal variable structure */
  17. void free_288(Real_288 *global)
  18. {
  19.     if (!global) return;
  20.     free(global);
  21. }
  22.  
  23. /* Unlike the 14.4 format, 28.8 blocks are interleaved */
  24. /* to dilute the effects of transmission errors */
  25. void deinterleave(unsigned char *in, unsigned char *out, unsigned int size)
  26. {
  27.   unsigned int x=0,y=0,z=0;
  28.   if (size>=38) z=size-38;
  29.   else return;
  30.  
  31.   while (x<=z)
  32.   {
  33.     memcpy(out+y,in+x,38);
  34.     x+=38;y+=456;
  35.     if (y>z) y-=z;
  36.   }
  37. }
  38.  
  39. /* Decode a block (celp) */
  40. void decode_288(Real_288 *global, unsigned char *in, signed short int *out)
  41. {
  42.   int x,y;
  43.   unsigned short int buffer[32];
  44.   Real_internal *glob;
  45.  
  46.   if (!global) return;
  47.   glob = (Real_internal *)global;
  48.  
  49.   unpack(buffer,in,32);
  50.   for (x=0;x<32;x++)
  51.   {
  52.     glob->phasep=(glob->phase=x&7)*5;
  53.     decode(glob,buffer[x]);
  54.     for (y=0;y<5;*(out++)=8*glob->output[glob->phasep+(y++)]);
  55.     if (glob->phase==3) update(glob);
  56.   }
  57. }
  58.  
  59. /* initial decode */
  60. static void unpack(unsigned short *tgt, unsigned char *src, int len)
  61. {
  62.   int x,y,z;
  63.   int n,temp;
  64.   int buffer[38];
  65.  
  66.   for (x=0;x<len;tgt[x++]=0)
  67.     buffer[x]=9+(x&1);
  68.  
  69.   for (x=y=z=0;x<38;x++) {
  70.     n=buffer[y]-z;
  71.     temp=src[x];
  72.     if (n<8) temp&=255>>(8-n);
  73.     tgt[y]+=temp<<z;
  74.     if (n<=8) {
  75.       tgt[++y]+=src[x]>>n;
  76.       z=8-n;
  77.     } else z+=8;
  78.   }
  79. }
  80.  
  81. static void update(Real_internal *glob)
  82. {
  83.   int x,y;
  84.   float buffer1[40],temp1[37];
  85.   float buffer2[8],temp2[11];
  86.  
  87.   for (x=0,y=glob->phasep+5;x<40;buffer1[x++]=glob->output[(y++)%40]);
  88.   co(36,40,35,buffer1,temp1,glob->st1a,glob->st1b,table1);
  89.   if (pred(temp1,glob->st1,36))
  90.     colmult(glob->pr1,glob->st1,table1a,36);
  91.  
  92.   for (x=0,y=glob->phase+1;x<8;buffer2[x++]=glob->history[(y++)%8]);
  93.   co(10,8,20,buffer2,temp2,glob->st2a,glob->st2b,table2);
  94.   if (pred(temp2,glob->st2,10))
  95.     colmult(glob->pr2,glob->st2,table2a,10);
  96. }
  97.  
  98. /* Decode and produce output */
  99. static void decode(Real_internal *glob, unsigned int input)
  100. {
  101.   unsigned int x,y;
  102.   float f;
  103.   double sum,sumsum;
  104.   float *p1,*p2;
  105.   float buffer[5];
  106.   const float *table;
  107.  
  108.   for (x=36;x--;glob->sb[x+5]=glob->sb[x]);
  109.   for (x=5;x--;) {
  110.     p1=glob->sb+x;p2=glob->pr1;
  111.     for (sum=0,y=36;y--;sum-=(*(++p1))*(*(p2++)));
  112.     glob->sb[x]=sum;
  113.   }
  114.  
  115.   f=amptable[input&7];
  116.   table=codetable+(input>>3)*5;
  117.  
  118.   /* convert log and do rms */
  119.   for (sum=32,x=10;x--;sum-=glob->pr2[x]*glob->lhist[x]);
  120.   if (sum<0) sum=0; else if (sum>60) sum=60;
  121.  
  122. /*
  123.   if(FastTable)
  124.   {
  125.     sumsum=powsum[(int)(sum*5)]*f;
  126.     for (x=5;x--;buffer[x]=table[x]*sumsum);
  127.  
  128.     for (x=10;--x;glob->lhist[x]=glob->lhist[x-1]);
  129.     *glob->lhist=glob->history[glob->phase]=f;
  130.   }
  131.   else
  132.   {
  133. */
  134.     sumsum=exp(sum*0.1151292546497)*f;    /* pow(10.0,sum/20)*f */
  135.     for (sum=0,x=5;x--;) { buffer[x]=table[x]*sumsum; sum+=buffer[x]*buffer[x]; }
  136.     if ((sum/=5)<1) sum=1;
  137.  
  138.     /* shift and store */
  139.     for (x=10;--x;glob->lhist[x]=glob->lhist[x-1]);
  140.     *glob->lhist=glob->history[glob->phase]=10*log10(sum)-32;
  141. /*
  142.   }
  143. */
  144.  
  145.   for (x=1;x<5;x++) for (y=x;y--;buffer[x]-=glob->pr1[x-y-1]*buffer[y]);
  146.  
  147.   /* output */
  148.   for (x=0;x<5;x++) {
  149.     f=glob->sb[4-x]+buffer[x];
  150.     if (f>4095) f=4095; else if (f<-4095) f=-4095;
  151.     glob->output[glob->phasep+x]=glob->sb[4-x]=f;
  152.   }
  153. }
  154.  
  155. /* column multiply */
  156. static void colmult(float *tgt, float *m1, const float *m2, int n)
  157. {
  158.   while (n--)
  159.     *(tgt++)=(*(m1++))*(*(m2++));
  160. }
  161.  
  162. static int pred(float *in, float *tgt, int n)
  163. {
  164.   int x,y;
  165.   float *p1,*p2;
  166.   double f0,f1,f2;
  167.   float temp;
  168.  
  169.   if (in[n]==0) return 0;
  170.   if ((f0=*in)<=0) return 0;
  171.  
  172.   for (x=1;;x++) {
  173.     if (n<x) return 1;
  174.  
  175.     p1=in+x;
  176.     p2=tgt;
  177.     f1=*(p1--);
  178.     for (y=x;--y;f1+=(*(p1--))*(*(p2++)));
  179.  
  180.     p1=tgt+x-1;
  181.     p2=tgt;
  182.     *(p1--)=f2=-f1/f0;
  183.     for (y=x>>1;y--;) {
  184.       temp=*p2+*p1*f2;
  185.       *(p1--)+=*p2*f2;
  186.       *(p2++)=temp;
  187.     }
  188.     if ((f0+=f1*f2)<0) return 0;
  189.   }
  190. }
  191.  
  192. static void co(int n, int i, int j, float *in, float *out, float *st1, float *st2, const float *table)
  193. {
  194.   int a,b,c;
  195.   unsigned int x;
  196.   float *fp,*fp2;
  197.   float buffer1[37];
  198.   float buffer2[37];
  199.   float work[111];
  200.  
  201.   /* rotate and multiply */
  202.   c=(b=(a=n+i)+j)-i;
  203.   fp=st1+i;
  204.   for (x=0;x<b;x++) {
  205.     if (x==c) fp=in;
  206.     work[x]=*(table++)*(*(st1++)=*(fp++));
  207.   }
  208.   
  209.   prodsum(buffer1,work+n,i,n);
  210.   prodsum(buffer2,work+a,j,n);
  211.  
  212.   for (x=0;x<=n;x++) {
  213.     *st2=*st2*(0.5625)+buffer1[x];
  214.     out[x]=*(st2++)+buffer2[x];
  215.   }
  216.   *out*=1.00390625; /* to prevent clipping */
  217. }
  218.  
  219. /* product sum (lsf) */
  220. static void prodsum(float *tgt, float *src, int len, int n)
  221. {
  222.   unsigned int x;
  223.   float *p1,*p2;
  224.   double sum;
  225.  
  226.   while (n>=0)
  227.   {
  228.     p1=(p2=src)-n;
  229.     for (sum=0,x=len;x--;sum+=(*p1++)*(*p2++));
  230.     tgt[n--]=sum;
  231.   }
  232. }
  233.