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

📄 aac_enc_quantization_fp.c

📁 audio-video-codecs.rar语音编解码器
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
//
//                  INTEL CORPORATION PROPRIETARY INFORMATION
//     This software is supplied under the terms of a license agreement or
//     nondisclosure agreement with Intel Corporation and may not be copied
//     or disclosed except in accordance with the terms of that agreement.
//          Copyright(c) 2003-2007 Intel Corporation. All Rights Reserved.
//
//     Intel Integrated Performance Primitives AAC Encode Sample for Windows*
//
//  By downloading and installing this sample, you hereby agree that the
//  accompanying Materials are being provided to you under the terms and
//  conditions of the End User License Agreement for the Intel Integrated
//  Performance Primitives product previously accepted by you. Please refer
//  to the file ippEULA.rtf or ippEULA.txt located in the root directory of your Intel IPP
//  product installation for more information.
//
//  MPEG-4 and AAC are international standards promoted by ISO, IEC, ITU, ETSI
//  and other organizations. Implementations of these standards, or the standard
//  enabled platforms may require licenses from various entities, including
//  Intel Corporation.
//
*/

#include <math.h>
#include <stdio.h>
#include "ippac.h"
#include "ipps.h"
#include "aac_enc_quantization_fp.h"
#include "aac_enc_search.h"

extern Ipp32u sf_huff_codebook[];

/****************************************************************************/

void main_loop(sQuantizationBlock* pBlock,
               sEnc_individual_channel_stream* pStream,
               Ipp32f* mdct_line,
               Ipp32f* mdct_scaled,
               Ipp32s* bitsForScaleFactorData);

void aac_FindSF(sEnc_individual_channel_stream* pStream,
                sQuantizationData* qData,
                Ipp32f* mdct_scaled,
                Ipp32f* mdct_line_abs,
                Ipp32s* startSF,
                Ipp32s* finishSF,
                Ipp16s* scalefac,
                Ipp16s* maxXQuant);

void aac_UpdateSF(sEnc_individual_channel_stream* pStream,
                  Ipp32f* mdct_scaled,
                  Ipp32f* mdct_line_abs,
                  Ipp32s* startSF,
                  Ipp16s* scalefac,
                  Ipp16s* maxXQuant,
                  Ipp32s  minSf,
                  Ipp32f  bitsToPeCoeff,
                  Ipp32f  possibleAddBits);

/****************************************************************************/

static Ipp32f scalefac_pow[] = {
  (Ipp32f)4.408719e+005, (Ipp32f)3.871411e+005, (Ipp32f)3.399587e+005,
    (Ipp32f)2.985266e+005, (Ipp32f)2.621440e+005, (Ipp32f)2.301955e+005,
    (Ipp32f)2.021407e+005, (Ipp32f)1.775050e+005, (Ipp32f)1.558718e+005,
    (Ipp32f)1.368751e+005, (Ipp32f)1.201936e+005, (Ipp32f)1.055451e+005,
    (Ipp32f)9.268190e+004, (Ipp32f)8.138639e+004, (Ipp32f)7.146751e+004,
    (Ipp32f)6.275749e+004, (Ipp32f)5.510899e+004, (Ipp32f)4.839264e+004,
    (Ipp32f)4.249484e+004, (Ipp32f)3.731583e+004, (Ipp32f)3.276800e+004,
    (Ipp32f)2.877444e+004, (Ipp32f)2.526758e+004, (Ipp32f)2.218812e+004,
    (Ipp32f)1.948397e+004, (Ipp32f)1.710938e+004, (Ipp32f)1.502419e+004,
    (Ipp32f)1.319314e+004, (Ipp32f)1.158524e+004, (Ipp32f)1.017330e+004,
    (Ipp32f)8.933439e+003, (Ipp32f)7.844686e+003, (Ipp32f)6.888623e+003,
    (Ipp32f)6.049080e+003, (Ipp32f)5.311855e+003, (Ipp32f)4.664478e+003,
    (Ipp32f)4.096000e+003, (Ipp32f)3.596804e+003, (Ipp32f)3.158448e+003,
    (Ipp32f)2.773515e+003, (Ipp32f)2.435496e+003, (Ipp32f)2.138673e+003,
    (Ipp32f)1.878024e+003, (Ipp32f)1.649142e+003, (Ipp32f)1.448155e+003,
    (Ipp32f)1.271662e+003, (Ipp32f)1.116680e+003, (Ipp32f)9.805858e+002,
    (Ipp32f)8.610779e+002, (Ipp32f)7.561350e+002, (Ipp32f)6.639819e+002,
    (Ipp32f)5.830598e+002, (Ipp32f)5.120000e+002, (Ipp32f)4.496006e+002,
    (Ipp32f)3.948060e+002, (Ipp32f)3.466894e+002, (Ipp32f)3.044370e+002,
    (Ipp32f)2.673341e+002, (Ipp32f)2.347530e+002, (Ipp32f)2.061428e+002,
    (Ipp32f)1.810193e+002, (Ipp32f)1.589578e+002, (Ipp32f)1.395850e+002,
    (Ipp32f)1.225732e+002, (Ipp32f)1.076347e+002, (Ipp32f)9.451687e+001,
    (Ipp32f)8.299773e+001, (Ipp32f)7.288247e+001, (Ipp32f)6.400000e+001,
    (Ipp32f)5.620007e+001, (Ipp32f)4.935075e+001, (Ipp32f)4.333618e+001,
    (Ipp32f)3.805463e+001, (Ipp32f)3.341676e+001, (Ipp32f)2.934413e+001,
    (Ipp32f)2.576785e+001, (Ipp32f)2.262742e+001, (Ipp32f)1.986972e+001,
    (Ipp32f)1.744812e+001, (Ipp32f)1.532165e+001, (Ipp32f)1.345434e+001,
    (Ipp32f)1.181461e+001, (Ipp32f)1.037472e+001, (Ipp32f)9.110309e+000,
    (Ipp32f)8.000000e+000, (Ipp32f)7.025009e+000, (Ipp32f)6.168843e+000,
    (Ipp32f)5.417022e+000, (Ipp32f)4.756828e+000, (Ipp32f)4.177095e+000,
    (Ipp32f)3.668016e+000, (Ipp32f)3.220981e+000, (Ipp32f)2.828427e+000,
    (Ipp32f)2.483716e+000, (Ipp32f)2.181015e+000, (Ipp32f)1.915207e+000,
    (Ipp32f)1.681793e+000, (Ipp32f)1.476826e+000, (Ipp32f)1.296840e+000,
    (Ipp32f)1.138789e+000, (Ipp32f)1.000000e+000, (Ipp32f)8.781261e-001,
    (Ipp32f)7.711054e-001, (Ipp32f)6.771278e-001, (Ipp32f)5.946036e-001,
    (Ipp32f)5.221369e-001, (Ipp32f)4.585020e-001, (Ipp32f)4.026226e-001,
    (Ipp32f)3.535534e-001, (Ipp32f)3.104645e-001, (Ipp32f)2.726269e-001,
    (Ipp32f)2.394008e-001, (Ipp32f)2.102241e-001, (Ipp32f)1.846033e-001,
    (Ipp32f)1.621049e-001, (Ipp32f)1.423486e-001, (Ipp32f)1.250000e-001,
    (Ipp32f)1.097658e-001, (Ipp32f)9.638818e-002, (Ipp32f)8.464097e-002,
    (Ipp32f)7.432544e-002, (Ipp32f)6.526711e-002, (Ipp32f)5.731275e-002,
    (Ipp32f)5.032782e-002, (Ipp32f)4.419417e-002, (Ipp32f)3.880806e-002,
    (Ipp32f)3.407837e-002, (Ipp32f)2.992510e-002, (Ipp32f)2.627801e-002,
    (Ipp32f)2.307541e-002, (Ipp32f)2.026312e-002, (Ipp32f)1.779357e-002,
    (Ipp32f)1.562500e-002, (Ipp32f)1.372072e-002, (Ipp32f)1.204852e-002,
    (Ipp32f)1.058012e-002, (Ipp32f)9.290681e-003, (Ipp32f)8.158389e-003,
    (Ipp32f)7.164094e-003, (Ipp32f)6.290978e-003, (Ipp32f)5.524272e-003,
    (Ipp32f)4.851007e-003, (Ipp32f)4.259796e-003, (Ipp32f)3.740638e-003,
    (Ipp32f)3.284752e-003, (Ipp32f)2.884426e-003, (Ipp32f)2.532890e-003,
    (Ipp32f)2.224197e-003, (Ipp32f)1.953125e-003, (Ipp32f)1.715090e-003,
    (Ipp32f)1.506065e-003, (Ipp32f)1.322515e-003, (Ipp32f)1.161335e-003,
    (Ipp32f)1.019799e-003, (Ipp32f)8.955118e-004, (Ipp32f)7.863722e-004,
    (Ipp32f)6.905340e-004, (Ipp32f)6.063759e-004, (Ipp32f)5.324745e-004,
    (Ipp32f)4.675797e-004, (Ipp32f)4.105940e-004, (Ipp32f)3.605533e-004,
    (Ipp32f)3.166112e-004, (Ipp32f)2.780246e-004, (Ipp32f)2.441406e-004,
    (Ipp32f)2.143863e-004, (Ipp32f)1.882582e-004, (Ipp32f)1.653144e-004,
    (Ipp32f)1.451669e-004, (Ipp32f)1.274748e-004, (Ipp32f)1.119390e-004,
    (Ipp32f)9.829653e-005, (Ipp32f)8.631675e-005, (Ipp32f)7.579699e-005,
    (Ipp32f)6.655931e-005, (Ipp32f)5.844747e-005, (Ipp32f)5.132424e-005,
    (Ipp32f)4.506916e-005, (Ipp32f)3.957640e-005, (Ipp32f)3.475307e-005,
    (Ipp32f)3.051758e-005, (Ipp32f)2.679828e-005, (Ipp32f)2.353227e-005,
    (Ipp32f)2.066430e-005, (Ipp32f)1.814586e-005, (Ipp32f)1.593435e-005,
    (Ipp32f)1.399237e-005, (Ipp32f)1.228707e-005, (Ipp32f)1.078959e-005,
    (Ipp32f)9.474623e-006, (Ipp32f)8.319914e-006, (Ipp32f)7.305933e-006,
    (Ipp32f)6.415531e-006, (Ipp32f)5.633645e-006, (Ipp32f)4.947050e-006,
    (Ipp32f)4.344134e-006, (Ipp32f)3.814697e-006, (Ipp32f)3.349785e-006,
    (Ipp32f)2.941534e-006, (Ipp32f)2.583037e-006, (Ipp32f)2.268233e-006,
    (Ipp32f)1.991794e-006, (Ipp32f)1.749046e-006, (Ipp32f)1.535883e-006,
    (Ipp32f)1.348699e-006, (Ipp32f)1.184328e-006, (Ipp32f)1.039989e-006,
    (Ipp32f)9.132417e-007, (Ipp32f)8.019413e-007, (Ipp32f)7.042056e-007,
    (Ipp32f)6.183813e-007, (Ipp32f)5.430167e-007, (Ipp32f)4.768372e-007,
    (Ipp32f)4.187231e-007, (Ipp32f)3.676917e-007, (Ipp32f)3.228797e-007,
    (Ipp32f)2.835291e-007, (Ipp32f)2.489743e-007, (Ipp32f)2.186308e-007,
    (Ipp32f)1.919854e-007, (Ipp32f)1.685874e-007, (Ipp32f)1.480410e-007,
    (Ipp32f)1.299987e-007, (Ipp32f)1.141552e-007, (Ipp32f)1.002427e-007,
    (Ipp32f)8.802570e-008, (Ipp32f)7.729766e-008, (Ipp32f)6.787709e-008,
    (Ipp32f)5.960464e-008, (Ipp32f)5.234039e-008, (Ipp32f)4.596146e-008,
    (Ipp32f)4.035996e-008, (Ipp32f)3.544113e-008, (Ipp32f)3.112178e-008,
    (Ipp32f)2.732885e-008, (Ipp32f)2.399818e-008, (Ipp32f)2.107342e-008,
    (Ipp32f)1.850512e-008, (Ipp32f)1.624983e-008, (Ipp32f)1.426940e-008,
    (Ipp32f)1.253033e-008, (Ipp32f)1.100321e-008, (Ipp32f)9.662208e-009,
    (Ipp32f)8.484637e-009, (Ipp32f)7.450581e-009, (Ipp32f)6.542549e-009,
    (Ipp32f)5.745183e-009, (Ipp32f)5.044995e-009, (Ipp32f)4.430142e-009,
    (Ipp32f)3.890223e-009, (Ipp32f)3.416106e-009, (Ipp32f)2.999772e-009,
    (Ipp32f)2.634178e-009, (Ipp32f)2.313140e-009, (Ipp32f)2.031229e-009,
    (Ipp32f)1.783675e-009
};

/****************************************************************************/

void static aac_LimitSF(Ipp32f* mdct_scaled,
                        Ipp32s  size,
                        Ipp32s* startSF,
                        Ipp32s* finishSF)
{
  Ipp32f start_scalefac, finish_scalefac, max_mdct_line;

  startSF[0] = -SF_OFFSET;
  finishSF[0] = -SF_OFFSET;
  max_mdct_line = 0;

  ippsMax_32f(mdct_scaled, size, &max_mdct_line);

  if (max_mdct_line > 0) {
    start_scalefac = (Ipp32f)log(max_mdct_line/MAX_QUANT)/(Ipp32f)log(2);
    start_scalefac *= (Ipp32f)16/(Ipp32f)3;
    startSF[0] = (Ipp32s)floor((Ipp64f)start_scalefac);

    if (startSF[0] < start_scalefac) startSF[0]++;

    finish_scalefac = (Ipp32f)log(max_mdct_line/(1 - MAGIC_NUMBER))/(Ipp32f)log(2);
    finish_scalefac *= (Ipp32f)16/(Ipp32f)3;
    finishSF[0] = (Ipp32s)floor((Ipp64f)finish_scalefac);

    if (finishSF[0] < finish_scalefac) finishSF[0]++;

    finishSF[0]++;

    if (startSF[0] > 255 - SF_OFFSET) startSF[0] = 255 - SF_OFFSET;
    else if (startSF[0] < -SF_OFFSET) startSF[0] = -SF_OFFSET;

    if (finishSF[0] > 255 - SF_OFFSET) finishSF[0] = 255 - SF_OFFSET;
    else if (finishSF[0] < -SF_OFFSET) finishSF[0] = -SF_OFFSET;
  }
}

/****************************************************************************/

void aac_MinMaxSF(sEnc_individual_channel_stream* pStream,
                  Ipp32f* mdct_scaled,
                  Ipp32s* startSF,
                  Ipp16s* scalefac,
                  Ipp16s* maxXQuant,
                  Ipp32s* minSf,
                  Ipp32s* maxSf,
                  Ipp32s  maxSfDelta)
{
  Ipp32s *sfb_offset = pStream->sfb_offset;
  Ipp32s *sfb_width = pStream->sfb_width;
  Ipp32s numSfb = pStream->num_window_groups * pStream->max_sfb;
  int sfb, update;

  minSf[0] = 100000;
  maxSf[0] = -100000;

  for (sfb = 0; sfb < numSfb; sfb++) {
    if (maxXQuant[sfb] != 0) {
      if (scalefac[sfb] > maxSf[0]) maxSf[0] = scalefac[sfb];
      if (scalefac[sfb] < minSf[0]) minSf[0] = scalefac[sfb];
    } else {
      ippsZero_32f(mdct_scaled + sfb_offset[sfb], sfb_width[sfb]);
    }
  }

  update = 0;
  if ((maxSf[0] - minSf[0]) > maxSfDelta) {
    for (sfb = 0; sfb < numSfb; sfb++) {
      if (maxXQuant[sfb] != 0) {
        /* very big difference */
        if ((minSf[0] + maxSfDelta) < startSF[sfb]) {
          minSf[0] = startSF[sfb] - maxSfDelta;
          update = 1;
        }
      }
    }
  }

  if (update == 1) {
    for (sfb = 0; sfb < numSfb; sfb++) {
      /* re quantization */
      if (scalefac[sfb] < minSf[0]) {
        Ipp32f mdct_rqnt[N_LONG/2];
        Ipp16s x_quant_unsigned[N_LONG/2];
        Ipp32f sf = (Ipp32f)scalefac_pow[minSf[0] + SF_OFFSET];
        Ipp32f temp = (Ipp32f)(MAGIC_NUMBER - 0.5f)/sf;

        ippsAddC_32f(mdct_scaled + sfb_offset[sfb], temp, mdct_rqnt, sfb_width[sfb]);
        ippsMulC_Low_32f16s(mdct_rqnt, sf, x_quant_unsigned, sfb_width[sfb]);
        ippsMax_16s(x_quant_unsigned, sfb_width[sfb], maxXQuant + sfb);

        scalefac[sfb] = (Ipp16s)minSf[0];
      }
    }
  }

  if ((maxSf[0] - minSf[0]) > maxSfDelta) {
    for (sfb = 0; sfb < numSfb; sfb++) {
      if (maxXQuant[sfb] != 0) {
        if (scalefac[sfb] > minSf[0] + maxSfDelta)
          scalefac[sfb] = (Ipp16s)(minSf[0] + maxSfDelta);
      }
    }
    maxSf[0] = minSf[0] + maxSfDelta;
  }
}

/****************************************************************************/

void Quantization(sQuantizationBlock* pBlock,
                  sEnc_individual_channel_stream* pStream,
                  sQuantizationData* qData)
{
  Ipp32f  mdct_line_abs[N_LONG/2];
  Ipp32f  mdct_scaled[N_LONG/2];
  Ipp32f* mdct_line      = qData->mdct_line;
  Ipp32f* bitsToPeCoeff  = qData->bitsToPeCoeff;
  Ipp32f* scalefactorDataBits = qData->scalefactorDataBits;
  Ipp32s* sfb_offset = pStream->sfb_offset;
  Ipp32s* sfb_width = pStream->sfb_width;
  Ipp32s  win, sfb;
  Ipp32s  firstNonZeroSfb;
  Ipp32s  bitsForScaleFactorData;

  ippsAbs_32f(mdct_line, mdct_line_abs, pStream->max_line);
  ippsPow34_32f(mdct_line_abs, mdct_scaled, pStream->max_line);

⌨️ 快捷键说明

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