Advertisement
cr88192

BTAC1C Mini Decoder.

Dec 12th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.66 KB | None | 0 0
  1. static int bgbmid_ima_index_table[16] = {
  2.   -1, -1, -1, -1, 2, 4, 6, 8,
  3.   -1, -1, -1, -1, 2, 4, 6, 8
  4. };
  5.  
  6. static int bgbmid_ima_step_table[128] = {
  7.      7,     8,     9,    10,    11,    12,    13,    14,    16,    17,
  8.     19,    21,    23,    25,    28,    31,    34,    37,    41,    45,
  9.     50,    55,    60,    66,    73,    80,    88,    97,   107,   118,
  10.    130,   143,   157,   173,   190,   209,   230,   253,   279,   307,
  11.    337,   371,   408,   449,   494,   544,   598,   658,   724,   796,
  12.    876,   963,  1060,  1166,  1282,  1411,  1552,  1707,  1878,  2066,
  13.   2272,  2499,  2749,  3024,  3327,  3660,  4026,  4428,  4871,  5358,
  14.   5894,  6484,  7132,  7845,  8630,  9493, 10442, 11487, 12635, 13899,
  15.  15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  16. };
  17.  
  18. void BGBDT_SndBTAC1C2_DecodeBlockMono(
  19.     byte *ibuf, s16 *obuf, int len);
  20.  
  21. void BGBDT_SndBTAC1C2_DecodeMonoBlockStereo(
  22.     byte *ibuf, s16 *obuf, int len);
  23. void BGBDT_SndBTAC1C2_DecodeJointBlockStereo(
  24.     byte *ibuf, s16 *obuf, int len);
  25. void BGBDT_SndBTAC1C2_DecodeStereoBlockStereo(
  26.     byte *ibuf, s16 *obuf, int len);
  27.  
  28. void BGBDT_SndBTAC1C_DecodeBlockStereo(
  29.     byte *ibuf, s16 *obuf, int len)
  30. {
  31.     int i;
  32.    
  33.     i=ibuf[6];
  34.  
  35.     if(i<89)
  36.     {
  37.         BGBDT_SndBTAC1C2_DecodeStereoBlockStereo(ibuf, obuf, len);
  38.         return;
  39.     }
  40.  
  41.     if(i==89)
  42.     {
  43.         BGBDT_SndBTAC1C2_DecodeMonoBlockStereo(ibuf, obuf, len);
  44.         return;
  45.     }
  46.  
  47.     if((i>=128) && (i<217))
  48.     {
  49.         BGBDT_SndBTAC1C2_DecodeJointBlockStereo(ibuf, obuf, len);
  50.         return;
  51.     }
  52. }
  53.  
  54. void BGBDT_SndBTAC1C_DecodeBlockStereoLg2(
  55.     byte *ibuf, s16 *obuf, int lg2)
  56. {
  57.     int i, j, k, l;
  58.     BGBDT_SndBTAC1C_DecodeBlockStereo(ibuf, obuf, (1<<lg2)-16);
  59.    
  60.     l=1<<lg2;
  61.     for(i=l-1; i>0; i--)
  62.     {
  63.         j=(i+1)>>(lg2-4);
  64.         obuf[i*2+0]=obuf[(i-j)*2+0];
  65.         obuf[i*2+1]=obuf[(i-j)*2+1];
  66.     }
  67. }
  68.  
  69.  
  70. void BGBDT_SndBTAC1C_DecodeBlockMonoLg2(
  71.     byte *ibuf, s16 *obuf, int lg2)
  72. {
  73.     int i, j, k, l;
  74.  
  75.     BGBDT_SndBTAC1C2_DecodeBlockMono(ibuf, obuf, (1<<lg2)-8);
  76.    
  77.     l=1<<lg2;
  78.     for(i=l-1; i>0; i--)
  79.     {
  80.         j=(i+1)>>(lg2-3);
  81.         obuf[i]=obuf[i-j];
  82.     }
  83. }
  84.  
  85. int BGBDT_SndBTAC1C2_PredictSample(int *psamp, int idx, int pfcn)
  86. {
  87.     int pred, p0, p1;
  88.     int i;
  89.    
  90.     i=idx;
  91.  
  92.     switch(pfcn)
  93.     {
  94.     case 0:
  95.         pred=psamp[(i-1)&7];
  96.         break;
  97.     case 1:
  98.         pred=2*psamp[(i-1)&7]-psamp[(i-2)&7];
  99.         break;
  100.     case 2:
  101.         pred=(3*psamp[(i-1)&7]-psamp[(i-2)&7])>>1;
  102.         break;
  103.     case 3:
  104.         pred=(5*psamp[(i-1)&7]-psamp[(i-2)&7])>>2;
  105.         break;
  106.     case 4:
  107.         p0=(psamp[(i-1)&7]+psamp[(i-2)&7]);
  108.         p1=(psamp[(i-2)&7]+psamp[(i-3)&7]);
  109.         pred=p0-(p1>>1);
  110.         break;
  111.     case 5:
  112.         p0=(psamp[(i-1)&7]+psamp[(i-2)&7]);
  113.         p1=(psamp[(i-2)&7]+psamp[(i-3)&7]);
  114.         pred=(3*p0-p1)>>2;
  115.         break;
  116.     case 6:
  117.         p0=(psamp[(i-1)&7]+psamp[(i-2)&7]);
  118.         p1=(psamp[(i-2)&7]+psamp[(i-3)&7]);
  119.         pred=(5*p0-p1)>>3;
  120.         break;
  121.     case 7:
  122.         pred=(  18*psamp[(i-1)&7]-4*psamp[(i-2)&7]+
  123.                  3*psamp[(i-3)&7]-2*psamp[(i-4)&7]+
  124.                  1*psamp[(i-5)&7])/16;
  125.         break;
  126.     case 8:
  127.         pred=(  72*psamp[(i-1)&7]-16*psamp[(i-2)&7]+
  128.                 12*psamp[(i-3)&7]- 8*psamp[(i-4)&7]+
  129.                  5*psamp[(i-5)&7]- 3*psamp[(i-6)&7]+
  130.                  3*psamp[(i-7)&7]- 1*psamp[(i-8)&7])/64;
  131.         break;
  132.     case 9:
  133.         pred=(  76*psamp[(i-1)&7]-17*psamp[(i-2)&7]+
  134.                 10*psamp[(i-3)&7]- 7*psamp[(i-4)&7]+
  135.                  5*psamp[(i-5)&7]- 4*psamp[(i-6)&7]+
  136.                  4*psamp[(i-7)&7]- 3*psamp[(i-8)&7])/64;
  137.         break;
  138.     case 10:
  139.         p0=(psamp[(i-1)&7]+psamp[(i-2)&7]+psamp[(i-3)&7]+psamp[(i-4)&7]);
  140.         p1=(psamp[(i-5)&7]+psamp[(i-6)&7]+psamp[(i-7)&7]+psamp[(i-8)&7]);
  141.         pred=(5*p0-p1)>>4;
  142.         break;
  143.     case 11:
  144.         p0=(psamp[(i-1)&7]+psamp[(i-2)&7]+psamp[(i-3)&7]+psamp[(i-4)&7]);
  145.         p1=(psamp[(i-5)&7]+psamp[(i-6)&7]+psamp[(i-7)&7]+psamp[(i-8)&7]);
  146.         pred=(p0+p1)>>3;
  147.         break;
  148.  
  149.     default:
  150.         pred=0;
  151.         break;
  152.     }
  153.     return(pred);
  154. }
  155.  
  156. void BGBDT_SndBTAC1C2_DecodeBlockMono(
  157.     byte *ibuf, s16 *obuf, int len)
  158. {
  159.     int psamp[8];
  160.     int p0, p1, p2, p3;
  161.     int pred, pfcn, index, step, diff, uni, sni;
  162.     int i, j;
  163.    
  164.     pred=(s16)(ibuf[0]+(ibuf[1]<<8));
  165.     index=ibuf[2]&127;
  166.     pfcn=ibuf[3]&15;
  167.    
  168.     psamp[0]=pred;  psamp[1]=pred;
  169.     psamp[2]=pred;  psamp[3]=pred;
  170.     psamp[4]=pred;  psamp[5]=pred;
  171.     psamp[6]=pred;  psamp[7]=pred;
  172.    
  173.     step=bgbmid_ima_step_table[index&127];
  174.  
  175.     for(i=0; i<len; i++)
  176.     {
  177.         j=(ibuf[4+(i>>1)]>>((i&1)*4))&15;
  178.         uni=j;
  179.  
  180.         if(pfcn)
  181.             { pred=BGBDT_SndBTAC1C2_PredictSample(psamp, i, pfcn); }
  182.  
  183.         index=index+bgbmid_ima_index_table[uni];
  184.         index=(index<0)?0:((index>88)?88:index);
  185.         diff=((2*(uni&7)+1)*step)/8;
  186.         if(uni&8)diff=-diff;
  187.         pred=pred+diff;
  188.         step=bgbmid_ima_step_table[index];
  189.  
  190.         pred=(pred<(-32768))?(-32768):((pred>32767)?32767:pred);
  191.         obuf[i]=pred;
  192.         psamp[i&7]=pred;
  193.     }
  194. }
  195.  
  196. void BGBDT_SndBTAC1C2_DecodeMonoBlockStereo(
  197.     byte *ibuf, s16 *obuf, int len)
  198. {
  199.     int psamp[8];
  200.     int pred, index, step, diff, uni, sni, ofs;
  201.     int pfcn;
  202.     int lp, rp;
  203.     int i, j;
  204.    
  205.     pred=(s16)(ibuf[0]+(ibuf[1]<<8));
  206.     index=ibuf[2]&127;
  207.     ofs=(s16)(ibuf[4]+(ibuf[5]<<8));
  208.     pfcn=ibuf[3]&15;
  209.    
  210.     psamp[0]=pred;  psamp[1]=pred;
  211.     psamp[2]=pred;  psamp[3]=pred;
  212.     psamp[4]=pred;  psamp[5]=pred;
  213.     psamp[6]=pred;  psamp[7]=pred;
  214.    
  215.     step=bgbmid_ima_step_table[index&127];
  216.  
  217.     for(i=0; i<len; i++)
  218.     {
  219.         j=(ibuf[8+(i>>1)]>>((i&1)*4))&15;
  220.         uni=j;
  221.  
  222.         if(pfcn)
  223.             { pred=BGBDT_SndBTAC1C2_PredictSample(psamp, i, pfcn); }
  224.  
  225.         index=index+bgbmid_ima_index_table[uni];
  226.         index=(index<0)?0:((index>88)?88:index);
  227.         diff=((2*(uni&7)+1)*step)/8;
  228.         if(uni&8)diff=-diff;
  229.         pred=pred+diff;
  230.         step=bgbmid_ima_step_table[index];
  231.  
  232.         pred=(pred<(-32768))?(-32768):((pred>32767)?32767:pred);
  233.         rp=(pred<<1)-ofs;
  234.         lp=rp+ofs;
  235.  
  236.         lp=(lp<(-32768))?(-32768):((lp>32767)?32767:lp);
  237.         rp=(rp<(-32768))?(-32768):((rp>32767)?32767:rp);
  238.         obuf[i*2+0]=lp;
  239.         obuf[i*2+1]=rp;
  240.         psamp[i&7]=pred;
  241.     }
  242. }
  243.  
  244. void BGBDT_SndBTAC1C2_DecodeJointBlockStereo(
  245.     byte *ibuf, s16 *obuf, int len)
  246. {
  247.     int psamp[8];
  248.     int pred, index, step, diff, uni, sni, ofs;
  249.     int lp, rp, pfcn, ispf;
  250.     int p0, p1, p2, p3;
  251.     int i, j, k, l;
  252.    
  253.     /* decode center channel */
  254.     pred=(s16)(ibuf[0]+(ibuf[1]<<8));
  255.     index=ibuf[2]&127;
  256.     pfcn=ibuf[3]&15;
  257.     step=bgbmid_ima_step_table[index&127]; 
  258.     psamp[0]=pred;  psamp[1]=pred;
  259.     psamp[2]=pred;  psamp[3]=pred;
  260.     psamp[4]=pred;  psamp[5]=pred;
  261.     psamp[6]=pred;  psamp[7]=pred;
  262.    
  263.     ispf=(ibuf[3]!=0)|(ibuf[7]!=0);
  264.    
  265.     for(i=0; i<len; i++)
  266.     {
  267.         k=ibuf[8+(i>>2)*2+0]|((ibuf[8+(i>>2)*2+1])<<8);
  268.         j=(k>>((i&3)*3))&7;
  269.         uni=j<<1;
  270.         uni|=((uni&6)==6)&ispf;
  271.  
  272.         if(pfcn)
  273.             { pred=BGBDT_SndBTAC1C2_PredictSample(psamp, i, pfcn); }
  274.  
  275.         index=index+bgbmid_ima_index_table[uni];
  276.         index=(index<0)?0:((index>88)?88:index);
  277.         diff=((2*(uni&7)+1)*step)/8;
  278.         if(uni&8)diff=-diff;
  279.         pred=pred+diff;
  280.         step=bgbmid_ima_step_table[index];
  281.  
  282.         pred=(pred<(-32768))?(-32768):((pred>32767)?32767:pred);
  283.         obuf[i*2+0]=pred;
  284.         psamp[i&7]=pred;
  285.     }
  286.    
  287.     l=len>>2;
  288.  
  289.     /* decode side channel */
  290.     pred=(s16)(ibuf[4]+(ibuf[5]<<8));
  291.     index=ibuf[6]&127;
  292.     pfcn=ibuf[7]&15;
  293.     step=bgbmid_ima_step_table[index&127];
  294.     psamp[0]=pred;  psamp[1]=pred;
  295.     psamp[2]=pred;  psamp[3]=pred;
  296.     psamp[4]=pred;  psamp[5]=pred;
  297.     psamp[6]=pred;  psamp[7]=pred;
  298.    
  299.     for(i=0; i<l; i++)
  300.     {
  301.         k=ibuf[8+i*2+0]|((ibuf[8+i*2+1])<<8);
  302.         uni=(k>>12)&15;
  303.  
  304.         if(pfcn)
  305.             { pred=BGBDT_SndBTAC1C2_PredictSample(psamp, i, pfcn); }
  306.  
  307.         index=index+bgbmid_ima_index_table[uni];
  308.         index=(index<0)?0:((index>88)?88:index);
  309.         diff=((2*(uni&7)+1)*step)/8;
  310.         if(uni&8)diff=-diff;
  311.         pred=pred+diff;
  312.         step=bgbmid_ima_step_table[index];
  313.  
  314.         pred=(pred<(-32768))?(-32768):((pred>32767)?32767:pred);
  315.         obuf[i*8+1]=pred;
  316.         psamp[i&7]=pred;
  317.     }
  318.  
  319.     /* center/side -> stereo */
  320.     for(i=0; i<l; i++)
  321.     {
  322.         ofs=obuf[i*8+1];
  323.         for(j=0; j<4; j++)
  324.         {
  325.             k=(i*4+j)*2;
  326.             pred=obuf[k+0];
  327.             lp=pred+ofs; rp=lp-(ofs<<1);
  328.             lp=(lp<(-32768))?(-32768):((lp>32767)?32767:lp);
  329.             rp=(rp<(-32768))?(-32768):((rp>32767)?32767:rp);
  330.             obuf[k+0]=lp; obuf[k+1]=rp;
  331.         }
  332.     }
  333. }
  334.  
  335. void BGBDT_SndBTAC1C2_DecodeStereoBlockStereoI(
  336.     byte *ibuf, s16 *obuf, int len)
  337. {
  338.     int plsamp[8], prsamp[8];
  339.     int lpred, lindex, lstep, ldiff, luni, lsni;
  340.     int rpred, rindex, rstep, rdiff, runi, rsni;
  341.     int pfcn;
  342.     int i, j, k;
  343.    
  344.     lpred=(s16)(ibuf[0]+(ibuf[1]<<8));
  345.     rpred=(s16)(ibuf[4]+(ibuf[5]<<8));
  346.     lindex=ibuf[2];
  347.     rindex=ibuf[6];
  348.     pfcn=ibuf[3]&15;
  349.  
  350.     lstep=bgbmid_ima_step_table[lindex&127];
  351.     rstep=bgbmid_ima_step_table[rindex&127];
  352.  
  353.     plsamp[0]=lpred;    plsamp[1]=lpred;
  354.     plsamp[2]=lpred;    plsamp[3]=lpred;
  355.     plsamp[4]=lpred;    plsamp[5]=lpred;
  356.     plsamp[6]=lpred;    plsamp[7]=lpred;
  357.     prsamp[0]=rpred;    prsamp[1]=rpred;
  358.     prsamp[2]=rpred;    prsamp[3]=rpred;
  359.     prsamp[4]=rpred;    prsamp[5]=rpred;
  360.     prsamp[6]=rpred;    prsamp[7]=rpred;
  361.  
  362.     for(i=0; i<len; i++)
  363.     {
  364.         k=((i>>3)*8)+((i&7)>>1);
  365.         j=(ibuf[ 8+k]>>((i&1)*4))&15;
  366.         luni=j;
  367.  
  368.         j=(ibuf[12+k]>>((i&1)*4))&15;
  369.         runi=j;
  370.  
  371.         if(pfcn)
  372.         {
  373.             lpred=BGBDT_SndBTAC1C2_PredictSample(plsamp, i, pfcn);
  374.             rpred=BGBDT_SndBTAC1C2_PredictSample(prsamp, i, pfcn);
  375.         }
  376.  
  377.         lindex=lindex+bgbmid_ima_index_table[luni];
  378.         lindex=(lindex<0)?0:((lindex>88)?88:lindex);
  379.         ldiff=((2*(luni&7)+1)*lstep)/8;
  380.         if(luni&8)ldiff=-ldiff;
  381.         lpred=lpred+ldiff;
  382.         lstep=bgbmid_ima_step_table[lindex];
  383.  
  384.         rindex=rindex+bgbmid_ima_index_table[runi];
  385.         rindex=(rindex<0)?0:((rindex>88)?88:rindex);
  386.         rdiff=((2*(runi&7)+1)*rstep)/8;
  387.         if(runi&8)rdiff=-rdiff;
  388.         rpred=rpred+rdiff;
  389.         rstep=bgbmid_ima_step_table[rindex];
  390.  
  391.         lpred=(lpred<(-32768))?(-32768):((lpred>32767)?32767:lpred);
  392.         rpred=(rpred<(-32768))?(-32768):((rpred>32767)?32767:rpred);
  393.        
  394.         obuf[i*2+0]=lpred;
  395.         obuf[i*2+1]=rpred;
  396.         plsamp[i&7]=lpred;
  397.         prsamp[i&7]=rpred;
  398.     }
  399. }
  400.  
  401. void BGBDT_SndBTAC1C2_DecodeStereoBlockStereo(
  402.     byte *ibuf, s16 *obuf, int len)
  403. {
  404.     int i, j, k, l;
  405.  
  406.     BGBDT_SndBTAC1C2_DecodeStereoBlockStereoI(ibuf, obuf, len>>1);
  407.    
  408.     l=len>>1;
  409.     for(i=len-1; i>0; i--)
  410.     {
  411.         j=i>>1;
  412.         obuf[i*2+0]=obuf[j*2+0];
  413.         obuf[i*2+1]=obuf[j*2+1];
  414.     }
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement