⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dts_parser.cpp

📁 ac3的解码程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
      
      // Adjustment
      rscale *= scalefactor_adj[ch][sel];
      for (m = 0; m < 8; m++) 
        subband_samples[ch][l][m] *= rscale;
      
      /////////////////////////////////////////////////////
      // Inverse ADPCM if in prediction mode

      if (prediction_mode[ch][l])
      {
        int n;
        for (m = 0; m < 8; m++)
        {
          for (n = 1; n <= 4; n++)
          {
            if (m-n >= 0)
              subband_samples[ch][l][m] +=
                (adpcm_vb[prediction_vq[ch][l]][n-1] *
                 subband_samples[ch][l][m-n]/8192);
            else if (predictor_history)
              subband_samples[ch][l][m] +=
                (adpcm_vb[prediction_vq[ch][l]][n-1] *
                subband_samples_hist[ch][l][m-n+4]/8192);
          }
        }
      }
    } // for (l = 0; l < vq_start_subband[ch] ; l++)
        
    ///////////////////////////////////////////////////////
    // Decode VQ encoded high frequencies

    for (l = vq_start_subband[ch]; l < subband_activity[ch]; l++)
    {
      // 1 vector -> 32 samples but we only need the 8 samples
      // for this subsubframe.
      int m;
      
      //if (!debug_flag & 0x01)
      //{
      //  fprintf (stderr, "Stream with high frequencies VQ coding\n");
      //  debug_flag |= 0x01;
      //}
      
      for (m = 0; m < 8; m++)
      {
        subband_samples[ch][l][m] = 
          high_freq_vq_tbl[high_freq_vq[ch][l]][subsubframe*8+m]
          * (double)scale_factor[ch][l][0] / 16.0;
      }
    } // for (l = vq_start_subband[ch]; l < subband_activity[ch]; l++)
  } // for (ch = 0; ch < prim_channels; ch++)
    
  // Check for DSYNC after subsubframe
  if (aspf || subsubframe == subsubframes - 1)
  {
    if (0xFFFF == bs.get(16)) // 0xFFFF
    {
#ifdef DEBUG
      fprintf( stderr, "Got subframe DSYNC\n" );
#endif
    }
    else
    {
      fprintf( stderr, "Didn't get subframe DSYNC\n" );
    }
  }
  
  // Backup predictor history for adpcm
  for (ch = 0; ch < prim_channels; ch++)
  {
    for (l = 0; l < vq_start_subband[ch] ; l++)
    {
      int m;
      for (m = 0; m < 4; m++)
        subband_samples_hist[ch][l][m] =
        subband_samples[ch][l][4+m];
    }
  }
  
  /////////////////////////////////////////////////////////
  //           SYNTHESIS AND OUTPUT
  /////////////////////////////////////////////////////////

  static const int reorder[10][NCHANNELS] =
  {                    // nch | arrangement
    { 0 },             //  1  | A
    { 0, 1 },          //  2  | A + B (dual mono)
    { 0, 1 },          //  2  | L + R (stereo)
    { 0, 1 },          //  2  | (L+R) + (L-R) (sum-difference)
    { 0, 1 },          //  2  | LT + RT (left & right total)
    { 1, 0, 2 },       //  3  | C + L + R
    { 0, 1, 2 },       //  3  | L + R + S
    { 1, 0, 2, 3 },    //  4  | C + L + R + S
    { 0, 1, 2, 3 },    //  4  | L + R + SL + SR
    { 1, 0, 2, 3, 4 }  //  5  | C + L + R + SL + SR
  };

//    static double pcmr2level_tbl[8] =
//    {32768.0, 32768.0, 524288.0, 524288.0, 1.0, 8388608.0, 8388608.0, 1.0 };

  int base = (current_subframe * subsubframes + current_subsubframe) * 32 * 8;

  // 32 subbands QMF
  for (ch = 0; ch < prim_channels; ch++)
    qmf_32_subbands(ch,
      subband_samples[ch],
      samples[reorder[amode][ch]] + base,
//      pcmr2level_tbl[source_pcm_res]);
      32768.0);

  // Generate LFE samples for this subsubframe FIXME!!!
  if (lfe)
  {
    int lfe_samples = 2 * lfe * subsubframes;
   
    lfe_interpolation_fir (lfe, 2 * lfe,
      lfe_data + lfe_samples + 2 * lfe * subsubframe,
      samples[prim_channels] + base,
      8388608.0);
    // Outputs 20bits pcm samples
  }

  return true;
}



///////////////////////////////////////////////////////////////////////////////
//                         SUBFRAME FOOTER
///////////////////////////////////////////////////////////////////////////////

bool
DTSParser::parse_subframe_footer()
{
  int aux_data_count = 0, i;
  int lfe_samples;

  /////////////////////////////////////////////////////////
  // Unpack optional information

  // Time code stamp
  if (timestamp) 
    bs.get(32);

  // Auxiliary data byte count
  if (aux_data) 
    aux_data_count = bs.get(6);

  // Auxiliary data bytes
  for(i = 0; i < aux_data_count; i++)
    bs.get(8);

  // Optional CRC check bytes
  if (crc_present && (downmix || dynrange))
    bs.get(16);

  // Backup LFE samples history
  lfe_samples = 2 * lfe * subsubframes;
  for (i = 0; i < lfe_samples; i++)
    lfe_data[i] = lfe_data[i+lfe_samples];

  return true;
}


int 
DTSParser::InverseQ(const huff_entry_t *huff)
{
  int value = 0;
  int length = 0, j;
  
  while (1)
  {
    length++;
    value <<= 1;
    value |= bs.get(1);
    
    for (j = 0; huff[j].length != 0 && huff[j].length < length; j++ );
    
    if (huff[j].length == 0) 
      break;
    
    for (; huff[j].length == length; j++)
      if (huff[j].code == value)
        return huff[j].value;
  }
  
  return 0;
}


void 
DTSParser::qmf_32_subbands (int ch, double samples_in[32][8], sample_t *samples_out,
                            double scale)
{           
  const double *prCoeff;
  int i, j, k;
  double raXin[32];

  double *subband_fir  = subband_fir_hist[ch];
  double *subband_fir2 = subband_fir_noidea[ch];

  int nChIndex = 0, NumSubband = 32, nStart = 0, nEnd = 8, nSubIndex;

  // Select filter
  if (!multirate_inter) 
    // Non-perfect reconstruction
    prCoeff = fir_32bands_nonperfect;
  else 
    // Perfect reconstruction
    prCoeff = fir_32bands_perfect;

  // Reconstructed channel sample index
  scale = 1.0 / scale;
  for (nSubIndex = nStart; nSubIndex < nEnd; nSubIndex++)
  {
    // Load in one sample from each subband
    for (i = 0; i < subband_activity[ch]; i++)
      raXin[i] = samples_in[i][nSubIndex];

    // Clear inactive subbands
    for (i = subband_activity[ch]; i < NumSubband; i++)
      raXin[i] = 0.0;

    sample_t a, b;
    for (j = 0, k = 0; k < 16; k++, j += 16)
    {
      a  = (raXin[0]  + raXin[1])  * cos_mod[j+0];
      a += (raXin[2]  + raXin[3])  * cos_mod[j+1];
      a += (raXin[4]  + raXin[5])  * cos_mod[j+2];
      a += (raXin[6]  + raXin[7])  * cos_mod[j+3];
      a += (raXin[8]  + raXin[9])  * cos_mod[j+4];
      a += (raXin[10] + raXin[11]) * cos_mod[j+5];
      a += (raXin[12] + raXin[13]) * cos_mod[j+6];
      a += (raXin[14] + raXin[15]) * cos_mod[j+7];
      a += (raXin[16] + raXin[17]) * cos_mod[j+8];
      a += (raXin[18] + raXin[19]) * cos_mod[j+9];
      a += (raXin[20] + raXin[21]) * cos_mod[j+10];
      a += (raXin[22] + raXin[23]) * cos_mod[j+11];
      a += (raXin[24] + raXin[25]) * cos_mod[j+12];
      a += (raXin[26] + raXin[27]) * cos_mod[j+13];
      a += (raXin[28] + raXin[29]) * cos_mod[j+14];
      a += (raXin[30] + raXin[31]) * cos_mod[j+15];

      b  = (raXin[0])              * cos_mod[256 + j+0];
      b += (raXin[2]  + raXin[1])  * cos_mod[256 + j+1];
      b += (raXin[4]  + raXin[3])  * cos_mod[256 + j+2];
      b += (raXin[6]  + raXin[5])  * cos_mod[256 + j+3];
      b += (raXin[8]  + raXin[7])  * cos_mod[256 + j+4];
      b += (raXin[10] + raXin[9])  * cos_mod[256 + j+5];
      b += (raXin[12] + raXin[11]) * cos_mod[256 + j+6];
      b += (raXin[14] + raXin[13]) * cos_mod[256 + j+7];
      b += (raXin[16] + raXin[15]) * cos_mod[256 + j+8];
      b += (raXin[18] + raXin[17]) * cos_mod[256 + j+9];
      b += (raXin[20] + raXin[19]) * cos_mod[256 + j+10];
      b += (raXin[22] + raXin[21]) * cos_mod[256 + j+11];
      b += (raXin[24] + raXin[23]) * cos_mod[256 + j+12];
      b += (raXin[26] + raXin[25]) * cos_mod[256 + j+13];
      b += (raXin[28] + raXin[27]) * cos_mod[256 + j+14];
      b += (raXin[30] + raXin[29]) * cos_mod[256 + j+15];

      subband_fir[k]      = cos_mod[k + 512] * (a+b);
      subband_fir[32-k-1] = cos_mod[k + 528] * (a-b);
    }

    // Multiply by filter coefficients
    for (k = 31, i = 0; i < 32; i++, k--)
    {
      a = subband_fir2[i];
      b = subband_fir2[32+i];

      a += prCoeff[i] * (subband_fir[i] - subband_fir[k]);
      b += prCoeff[32+i]*(-subband_fir[i] - subband_fir[k]);
      a += prCoeff[i+64] * (subband_fir[i+64] - subband_fir[k+64]);
      b += prCoeff[32+i+64]*(-subband_fir[i+64] - subband_fir[k+64]);
      a += prCoeff[i+128] * (subband_fir[i+128] - subband_fir[k+128]);
      b += prCoeff[32+i+128]*(-subband_fir[i+128] - subband_fir[k+128]);
      a += prCoeff[i+192] * (subband_fir[i+192] - subband_fir[k+192]);
      b += prCoeff[32+i+192]*(-subband_fir[i+192] - subband_fir[k+192]);
      a += prCoeff[i+256] * (subband_fir[i+256] - subband_fir[k+256]);
      b += prCoeff[32+i+256]*(-subband_fir[i+256] - subband_fir[k+256]);
      a += prCoeff[i+320] * (subband_fir[i+320] - subband_fir[k+320]);
      b += prCoeff[32+i+320]*(-subband_fir[i+320] - subband_fir[k+320]);
      a += prCoeff[i+384] * (subband_fir[i+384] - subband_fir[k+384]);
      b += prCoeff[32+i+384]*(-subband_fir[i+384] - subband_fir[k+384]);
      a += prCoeff[i+448] * (subband_fir[i+448] - subband_fir[k+448]);
      b += prCoeff[32+i+448]*(-subband_fir[i+448] - subband_fir[k+448]);

      subband_fir2[i] = a;
      subband_fir2[32+i] = b;
    }

    // Create 32 PCM output samples
    for (i = 0; i < 32; i++)
      samples_out[nChIndex++] = subband_fir2[i] * scale;

    // Update working arrays
    for (i = 512-16; i >= 32; i -= 16)
    {
      subband_fir[i+0]  = subband_fir[i-32+0];
      subband_fir[i+1]  = subband_fir[i-32+1];
      subband_fir[i+2]  = subband_fir[i-32+2];
      subband_fir[i+3]  = subband_fir[i-32+3];
      subband_fir[i+4]  = subband_fir[i-32+4];
      subband_fir[i+5]  = subband_fir[i-32+5];
      subband_fir[i+6]  = subband_fir[i-32+6];
      subband_fir[i+7]  = subband_fir[i-32+7];
      subband_fir[i+8]  = subband_fir[i-32+8];
      subband_fir[i+9]  = subband_fir[i-32+9];
      subband_fir[i+10] = subband_fir[i-32+10];
      subband_fir[i+11] = subband_fir[i-32+11];
      subband_fir[i+12] = subband_fir[i-32+12];
      subband_fir[i+13] = subband_fir[i-32+13];
      subband_fir[i+14] = subband_fir[i-32+14];
      subband_fir[i+15] = subband_fir[i-32+15];
    }

    for (i = 0; i < NumSubband; i++)
      subband_fir2[i] = subband_fir2[i+32];

    for (i = 0; i < NumSubband; i++)
      subband_fir2[i+32] = 0.0;

  } // for (nSubIndex=nStart; nSubIndex<nEnd; nSubIndex++)
}

void 
DTSParser::lfe_interpolation_fir(int nDecimationSelect, int nNumDeciSample,
                                 double *samples_in, sample_t *samples_out,
                                 double scale)
{
  // samples_in: An array holding decimated samples.
  //   Samples in current subframe starts from samples_in[0],
  //   while samples_in[-1], samples_in[-2], ..., stores samples
  //   from last subframe as history.
  //
  // samples_out: An array holding interpolated samples
  //

  int nDeciFactor, k, J;
  const double *prCoeff;

  int NumFIRCoef = 512; // Number of FIR coefficients
  int nInterpIndex = 0; // Index to the interpolated samples
  int nDeciIndex;

  // Select decimation filter
  if (nDecimationSelect == 1)
  {
    // 128 decimation
    nDeciFactor = 128;      
    prCoeff = lfe_fir_128;
  } 
  else 
  {
    // 64 decimation
    nDeciFactor = 64;
    prCoeff = lfe_fir_64;
  }

  // Interpolation
  for (nDeciIndex = 0; nDeciIndex < nNumDeciSample; nDeciIndex++)
  {
    // One decimated sample generates nDeciFactor interpolated ones
    for (k = 0; k < nDeciFactor; k++)
    {
      double rTmp = 0.0;
      for (J = 0; J < NumFIRCoef/nDeciFactor; J++)
        rTmp += samples_in[nDeciIndex-J] * prCoeff[k+J*nDeciFactor];
      // Save interpolated samples
      samples_out[nInterpIndex++] = rTmp / scale;
    }
  }
}






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -