cr88192

BLTAC1A Enc/Dec 0

Sep 7th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1. /*
  2. 16 samples in 32 bits:
  3.   [31:24]=max-min
  4.   [23:16]=(max+min)/2
  5.   [15: 0]=sample bits
  6.  
  7. Here, the block is unpacked into a high and low value, and each bit selects either max(1) or min(0).
  8. This is tuned more for decoder simplicity than goodness.
  9.  
  10.  
  11. 16 samples in 64 bits:
  12.   [63:54]=predictor sample (high 10 bits of 16-bit sample)
  13.   [53:48]=step bits
  14.   [47: 0]=sample bits (16x 3b)
  15.  
  16. Each sample is a delta relative to a predictor scaled according to the a step.
  17. The step is adjusted based on the sample bits, and is essentially a 4.2 microfloat.
  18.  
  19. Each sample has 3 bits:
  20.   2: sign
  21.   1: diff+=step>>1
  22.   0: diff+=step>>2
  23.  
  24. */
  25.  
  26. #if 1
  27. int bltac_encblock(s16 *iblk, u32 *oblk)
  28. {
  29.     u32 blk;
  30.     int m, n, a, b, c, d;
  31.     int n2, m2, c0, c1, d1;
  32.     int s0, s1;
  33.     int i, j, k;
  34.    
  35.     m=65536;
  36.     n=-65536;
  37.     for(i=0; i<16; i++)
  38.     {
  39.         k=iblk[i];
  40.         if(k<m)m=k;
  41.         if(k>n)n=k;
  42.     }
  43.  
  44.     a=(m+n)/2;
  45.     m=(m+a)/2;
  46.     n=(n+a)/2;
  47.  
  48.     m=(3*m+a)/4;
  49.     n=(3*n+a)/4;
  50.    
  51.     c=(m+n)/2;
  52.     d=n-m;
  53.  
  54.     c0=(c>>8)+128;
  55.     c=clamp255(((c+127)>>8)+128);
  56.     d=clamp255(d>>8);
  57.  
  58.     c1=(c-128)<<8;
  59.     d1=d<<8;
  60.    
  61.     m=c1-(d1>>1);
  62.     n=c1+d1;
  63.    
  64.     if(((n<m) || (n>32767) || (m<-32767)) && (c!=c0))
  65.     {
  66.         c=c0;
  67.         c1=(c-128)<<8;  d1=d<<8;
  68.         m=c1-(d1>>1);   n=c1+d1;
  69.     }
  70.  
  71.     while(((n<m) || (n>32767) || (m<-32767)) && (d>0))
  72.     {
  73.         d--;
  74.         c1=(c-128)<<8;  d1=d<<8;
  75.         m=c1-(d1>>1);   n=c1+d1;
  76.     }
  77.    
  78.     b=0; a=c1;
  79.     for(i=0; i<16; i++)
  80.     {
  81.         k=iblk[i];
  82.  
  83.         if(k>a)
  84.         {
  85.             b|=1<<i;
  86.             a=(3*a+n)/4;
  87.         }else
  88.         {
  89.             a=(3*a+m)/4;
  90.         }
  91.     }
  92.    
  93.     blk=b|(c<<16)|(d<<24);
  94.     *oblk=blk;
  95.  
  96.     return(0);
  97. }
  98.  
  99. int bltac_decblock(u32 blk, s16 *oblk)
  100. {
  101.     int m, n, a, b, c, d;
  102.     int i, j, k;
  103.    
  104.     a=(blk>>16)&255;
  105.     b=(blk>>24)&255;
  106.     c=(a-128)<<8;
  107.     d=b<<8;
  108.     m=c-(d>>1);
  109.     n=c+d;
  110.  
  111.     b=blk&65535;
  112.     for(i=0; i<16; i++)
  113.         { oblk[i]=(b&(1<<i))?n:m; }
  114.  
  115.     return(0);
  116. }
  117. #endif
  118.  
  119.  
  120. #if 1
  121. int bltac_encblock2(s16 *iblk, u64 *oblk)
  122. {
  123.     static int la=0;
  124.     static int ls=0;
  125.     static u16 *lpn=NULL;
  126.     u64 blk;
  127.     int m, n, a, b, c, d, s;
  128.     int n2, m2, c0, c1, d1;
  129.     int s0, s1;
  130.     int i, j, k, l;
  131.    
  132.     if(iblk==lpn)
  133.     {
  134.         s0=(la&0xFFC0)|ls;
  135.         s1=ls;
  136.     }else
  137.     {
  138.         la=(iblk[0]+iblk[1])/2;
  139.         ls=8;
  140.         s0=(la&0xFFC0)|ls;
  141.         s1=ls;
  142.     }
  143.  
  144.     a=(s16)(s0&0xFFC0);
  145.     s=s0&63;
  146.     blk=((u64)s0)<<48;
  147.     for(i=0; i<16; i++)
  148.     {
  149.         k=(s16)(iblk[i]&0xFFC0);
  150.         c0=k-a;
  151.         c1=(c0>=0)?c0:(-c0);
  152.  
  153.         d=(4|(s&3))<<(s>>2);
  154.         b=(4*c1)/d;
  155.         b=clamp(b, 0, 3);
  156.         if(c0<0)b|=4;
  157.         blk|=((u64)b)<<(i*3);
  158.  
  159.         d1=d>>3;
  160.         if(b&1)d1+=d>>2;
  161.         if(b&2)d1+=d>>1;
  162.         if(b&4)d1=-d1;
  163.         s+=((b&3)==3)?3:((b&3)-1);
  164.         a+=d1;
  165.         a=clamp(a, -32768, 32767);
  166.         s=clamp(s, 0, 63);
  167.     }
  168.     la=a;
  169.     ls=s;
  170.     lpn=iblk+16;
  171.    
  172.     *oblk=blk;
  173. }
  174.    
  175. int bltac_decblock2(u64 blk, s16 *oblk)
  176. {
  177.     int m, n, a, b, c, d, s;
  178.     int n2, m2, c0, c1, d1;
  179.     int s0, s1;
  180.     int i, j, k, l;
  181.    
  182.     s0=(s16)(blk>>48);
  183.     a=(s16)(s0&0xFFC0);
  184.     s=s0&63;
  185.    
  186.     for(i=0; i<16; i++)
  187.     {
  188.         b=(blk>>(i*3))&7;
  189.         d=(4|(s&3))<<(s>>2);
  190.         d1=d>>3;
  191.         if(b&1)d1+=d>>2;
  192.         if(b&2)d1+=d>>1;
  193.         if(b&4)d1=-d1;
  194.         s+=((b&3)==3)?3:((b&3)-1);
  195.         a+=d1;
  196.         a=clamp(a, -32768, 32767);
  197.         s=clamp(s, 0, 63);
  198.         oblk[i]=a;
  199.     }
  200. }
  201. #endif
Add Comment
Please, Sign In to add comment