home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Audio / AC3 / AC3Dec / coeff.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  8.2 KB  |  354 lines

  1. /* 
  2.  *    coeff.c
  3.  *
  4.  *    Copyright (C) Aaron Holtzman - May 1999
  5.  *
  6.  *  This file is part of ac3dec, a free Dolby AC-3 stream decoder.
  7.  *    
  8.  *  ac3dec is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  ac3dec is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include "ac3.h"
  28. #include "ac3_internal.h"
  29.  
  30.  
  31. #include "decode.h"
  32. #include "bitstream.h"
  33. #include "dither.h"
  34. #include "coeff.h"
  35.  
  36. //
  37. //Lookup tables of 0.15 two's complement quantization values
  38. //
  39. static const uint_16 q_1[3] = 
  40. {
  41.     ( -2 << 15)/3, 0,(  2 << 15)/3 
  42. };
  43.  
  44. static const uint_16 q_2[5] = 
  45. {
  46.     ( -4 << 15)/5,( -2 << 15)/5, 0,
  47.     (  2 << 15)/5,(  4 << 15)/5
  48. };
  49.  
  50. static const uint_16 q_3[7] = 
  51. {
  52.     ( -6 << 15)/7,( -4 << 15)/7,( -2 << 15)/7, 0,
  53.     (  2 << 15)/7,(  4 << 15)/7,(  6 << 15)/7
  54. };
  55.  
  56. static const uint_16 q_4[11] = 
  57. {
  58.     (-10 << 15)/11,(-8 << 15)/11,(-6 << 15)/11, ( -4 << 15)/11,(-2 << 15)/11,  0,
  59.     (  2 << 15)/11,( 4 << 15)/11,( 6 << 15)/11, (  8 << 15)/11,(10 << 15)/11
  60. };
  61.  
  62. static const uint_16 q_5[15] = 
  63. {
  64.     (-14 << 15)/15,(-12 << 15)/15,(-10 << 15)/15,
  65.     ( -8 << 15)/15,( -6 << 15)/15,( -4 << 15)/15,
  66.     ( -2 << 15)/15,   0          ,(  2 << 15)/15,
  67.     (  4 << 15)/15,(  6 << 15)/15,(  8 << 15)/15,
  68.     ( 10 << 15)/15,( 12 << 15)/15,( 14 << 15)/15
  69. };
  70.  
  71. //
  72. // Scale factors for convert_to_float
  73. //
  74.  
  75. static const uint_32 u32_scale_factors[25] = 
  76. {
  77.     0x38000000, //2 ^ -(0 + 15)
  78.     0x37800000, //2 ^ -(1 + 15)
  79.     0x37000000, //2 ^ -(2 + 15)
  80.     0x36800000, //2 ^ -(3 + 15)
  81.     0x36000000, //2 ^ -(4 + 15)
  82.     0x35800000, //2 ^ -(5 + 15)
  83.     0x35000000, //2 ^ -(6 + 15)
  84.     0x34800000, //2 ^ -(7 + 15)
  85.     0x34000000, //2 ^ -(8 + 15)
  86.     0x33800000, //2 ^ -(9 + 15)
  87.     0x33000000, //2 ^ -(10 + 15)
  88.     0x32800000, //2 ^ -(11 + 15)
  89.     0x32000000, //2 ^ -(12 + 15)
  90.     0x31800000, //2 ^ -(13 + 15)
  91.     0x31000000, //2 ^ -(14 + 15)
  92.     0x30800000, //2 ^ -(15 + 15)
  93.     0x30000000, //2 ^ -(16 + 15)
  94.     0x2f800000, //2 ^ -(17 + 15)
  95.     0x2f000000, //2 ^ -(18 + 15)
  96.     0x2e800000, //2 ^ -(19 + 15)
  97.     0x2e000000, //2 ^ -(20 + 15)
  98.     0x2d800000, //2 ^ -(21 + 15)
  99.     0x2d000000, //2 ^ -(22 + 15)
  100.     0x2c800000, //2 ^ -(23 + 15)
  101.     0x2c000000  //2 ^ -(24 + 15)
  102. };
  103.  
  104. static float *scale_factor = (float*)u32_scale_factors;
  105.  
  106. //These store the persistent state of the packed mantissas
  107. static uint_16 m_1[3];
  108. static uint_16 m_2[3];
  109. static uint_16 m_4[2];
  110. static uint_16 m_1_pointer;
  111. static uint_16 m_2_pointer;
  112. static uint_16 m_4_pointer;
  113.  
  114. //Conversion from bap to number of bits in the mantissas
  115. //zeros account for cases 0,1,2,4 which are special cased
  116. static uint_16 qnttztab[16] = { 0, 0, 0, 3, 0 , 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16};
  117.  
  118. static void    coeff_reset(void);
  119. static sint_16 coeff_get_mantissa(uint_16 bap, uint_16 dithflag);
  120. static void    coeff_uncouple_ch(float samples[],bsi_t *bsi,audblk_t *audblk,uint_32 ch);
  121.  
  122. //
  123. // Convert a 0.15 fixed point number into IEEE single
  124. // precision floating point and scale by 2^-exp
  125. //
  126. static inline float
  127. convert_to_float(uint_16 exp, sint_16 mantissa)
  128. {
  129.     float x;
  130.  
  131.     //the scale by 2^-15 is built into the scale factor table
  132.     x = mantissa * scale_factor[exp];
  133.  
  134.     return x;
  135. }
  136.  
  137. void
  138. coeff_unpack(bsi_t *bsi, audblk_t *audblk, stream_samples_t samples)
  139. {
  140.     uint_16 i,j;
  141.     uint_32 done_cpl = 0;
  142.     sint_16 mantissa;
  143.  
  144.     coeff_reset();
  145.  
  146.     for(i=0; i< bsi->nfchans; i++)
  147.     {
  148.         for(j=0; j < audblk->endmant[i]; j++)
  149.         {
  150.             mantissa = coeff_get_mantissa(audblk->fbw_bap[i][j],audblk->dithflag[i]);
  151.             samples[i][j] = convert_to_float(audblk->fbw_exp[i][j],mantissa);
  152.         }
  153.  
  154.         if(audblk->cplinu && audblk->chincpl[i] && !(done_cpl))
  155.         {
  156.             // ncplmant is equal to 12 * ncplsubnd
  157.             // Don't dither coupling channel until channel separation so that
  158.             // interchannel noise is uncorrelated 
  159.             for(j=audblk->cplstrtmant; j < audblk->cplendmant; j++)
  160.                 audblk->cplmant[j] = coeff_get_mantissa(audblk->cpl_bap[j],0);
  161.             done_cpl = 1;
  162.         }
  163.     }
  164.  
  165.     //uncouple the channel if necessary
  166.     if(audblk->cplinu)
  167.     {
  168.         for(i=0; i< bsi->nfchans; i++)
  169.         {
  170.             if(audblk->chincpl[i])
  171.                 coeff_uncouple_ch(samples[i],bsi,audblk,i);
  172.         }
  173.  
  174.     }
  175.  
  176.     if(bsi->lfeon)
  177.     {
  178.         // There are always 7 mantissas for lfe, no dither for lfe 
  179.         for(j=0; j < 7 ; j++)
  180.         {
  181.             mantissa = coeff_get_mantissa(audblk->lfe_bap[j],0);
  182.             samples[5][j] = convert_to_float(audblk->lfe_exp[j],mantissa);
  183.         }
  184.     }
  185. }
  186.  
  187. //
  188. //Fetch a mantissa from the bitstream
  189. //
  190. //The mantissa returned is a signed 0.15 fixed point number
  191. //
  192. static sint_16
  193. coeff_get_mantissa(uint_16 bap, uint_16 dithflag)
  194. {
  195.     uint_16 mantissa;
  196.     uint_16 group_code;
  197.  
  198.     //If the bap is 0-5 then we have special cases to take care of
  199.     switch(bap)
  200.     {
  201.         case 0:
  202.             if(dithflag)
  203.                 mantissa = dither_gen();
  204.             else
  205.                 mantissa = 0;
  206.             break;
  207.  
  208.         case 1:
  209.             if(m_1_pointer > 2)
  210.             {
  211.                 group_code = bitstream_get(5);
  212.  
  213.                 if(group_code > 26)
  214.                     goto error;
  215.  
  216.                 m_1[0] = group_code / 9; 
  217.                 m_1[1] = (group_code % 9) / 3; 
  218.                 m_1[2] = (group_code % 9) % 3; 
  219.                 m_1_pointer = 0;
  220.             }
  221.             mantissa = m_1[m_1_pointer++];
  222.             mantissa = q_1[mantissa];
  223.             break;
  224.         case 2:
  225.  
  226.             if(m_2_pointer > 2)
  227.             {
  228.                 group_code = bitstream_get(7);
  229.  
  230.                 if(group_code > 124)
  231.                     goto error;
  232.  
  233.                 m_2[0] = group_code / 25;
  234.                 m_2[1] = (group_code % 25) / 5 ;
  235.                 m_2[2] = (group_code % 25) % 5 ; 
  236.                 m_2_pointer = 0;
  237.             }
  238.             mantissa = m_2[m_2_pointer++];
  239.             mantissa = q_2[mantissa];
  240.             break;
  241.  
  242.         case 3:
  243.             mantissa = bitstream_get(3);
  244.  
  245.             if(mantissa > 6)
  246.                 goto error;
  247.  
  248.             mantissa = q_3[mantissa];
  249.             break;
  250.  
  251.         case 4:
  252.             if(m_4_pointer > 1)
  253.             {
  254.                 group_code = bitstream_get(7);
  255.  
  256.                 if(group_code > 120)
  257.                     goto error;
  258.  
  259.                 m_4[0] = group_code / 11;
  260.                 m_4[1] = group_code % 11;
  261.                 m_4_pointer = 0;
  262.             }
  263.             mantissa = m_4[m_4_pointer++];
  264.             mantissa = q_4[mantissa];
  265.             break;
  266.  
  267.         case 5:
  268.             mantissa = bitstream_get(4);
  269.  
  270.             if(mantissa > 14)
  271.                 goto error;
  272.  
  273.             mantissa = q_5[mantissa];
  274.             break;
  275.  
  276.         default:
  277.             mantissa = bitstream_get(qnttztab[bap]);
  278.             mantissa <<= 16 - qnttztab[bap];
  279.     }
  280.  
  281.     return mantissa;
  282.  
  283.  
  284.  
  285. error:
  286.     if(!error_flag)
  287.         fprintf(stderr,"** Invalid mantissa - skipping frame **\n");
  288.     error_flag = 1;
  289.  
  290.     return 0;
  291. }
  292.  
  293. //
  294. // Reset the mantissa state
  295. //
  296. static void 
  297. coeff_reset(void)
  298. {
  299.     m_1[2] = m_1[1] = m_1[0] = 0;
  300.     m_2[2] = m_2[1] = m_2[0] = 0;
  301.     m_4[1] = m_4[0] = 0;
  302.     m_1_pointer = m_2_pointer = m_4_pointer = 3;
  303. }
  304.  
  305. //
  306. // Uncouple the coupling channel into a fbw channel
  307. //
  308. static void
  309. coeff_uncouple_ch(float samples[],bsi_t *bsi,audblk_t *audblk,uint_32 ch)
  310. {
  311.     uint_32 bnd = 0;
  312.     uint_32 sub_bnd = 0;
  313.     uint_32 i,j;
  314.     float cpl_coord = 1.0;
  315.     uint_32 cpl_exp_tmp;
  316.     uint_32 cpl_mant_tmp;
  317.     sint_16 mantissa;
  318.  
  319.  
  320.     for(i=audblk->cplstrtmant;i<audblk->cplendmant;)
  321.     {
  322.         if(!audblk->cplbndstrc[sub_bnd++])
  323.         {
  324.             cpl_exp_tmp = audblk->cplcoexp[ch][bnd] + 3 * audblk->mstrcplco[ch];
  325.             if(audblk->cplcoexp[ch][bnd] == 15)
  326.                 cpl_mant_tmp = (audblk->cplcomant[ch][bnd]) << 11;
  327.             else
  328.                 cpl_mant_tmp = ((0x10) | audblk->cplcomant[ch][bnd]) << 10;
  329.             
  330.             cpl_coord = convert_to_float(cpl_exp_tmp,cpl_mant_tmp) * 8.0f;
  331.  
  332.             //Invert the phase for the right channel if necessary
  333.             if(bsi->acmod == 0x2 && audblk->phsflginu && ch == 1 && audblk->phsflg[bnd])
  334.                 cpl_coord *= -1;
  335.  
  336.             bnd++;
  337.         }
  338.  
  339.         for(j=0;j < 12; j++)
  340.         {
  341.             //Get new dither values for each channel if necessary, so
  342.             //the channels are uncorrelated
  343.             if(audblk->dithflag[ch] && audblk->cpl_bap[i] == 0)
  344.                 mantissa = dither_gen();
  345.             else
  346.                 mantissa = audblk->cplmant[i];
  347.  
  348.             samples[i]  = cpl_coord * convert_to_float(audblk->cpl_exp[i],mantissa);
  349.  
  350.             i++;
  351.         }
  352.     }
  353. }
  354.