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

📄 mp3enc_quantization_int.c

📁 audio-video-codecs.rar语音编解码器
💻 C
📖 第 1 页 / 共 2 页
字号:
/*//////////////////////////////////////////////////////////////////////////////
//
//                  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) 2002-2007 Intel Corporation. All Rights Reserved.
//
*/

#include "mp3enc_own_int.h"

#define LEN_MDCT_LINE  576
#define SF_OFFSET      210
#define MAX_QUANT      8206
#define MAGIC_NUMBER_I 26568
#define QUANT_ITER_NUM 8

#define C_0_95_Q15 31129
#define C_0_85_Q15 27852
#define C_1_2_Q14  19660
#define C_0_2_Q15  6553
#define C_0_5_Q15  16384
#define C_0_6_Q15  19660

void mp3ienc_quantCalcAvailableBits(MP3Enc *state) {
    Ipp32s ch, gr;
    Ipp32s len;
    Ipp32s bitsPerFrame;
    Ipp32s mean_bits;
    Ipp32s bits_in_buf, max_bits_in_buf;
    Ipp32s bits_from_buffer, com_additional_bits;
    Ipp32s additional_bits[2][2];
//  Ipp32s up, l;
    Ipp32s div;
    IppMP3FrameHeader *header = &state->com.header;
    Ipp32s stereo = state->com.stereo;
    Ipp32s ms = state->com.stereo_mode == MPA_MS_STEREO ? 2 : 0;

    len = 32;   // header

    if (stereo == 1)
        len += 136;
    else
        len += 256;

    if (header->protectionBit)
        len += 16;

    state->com.frameBits = len;
    bitsPerFrame = state->com.slot_size << 3;

    max_bits_in_buf = 8*511;
    mean_bits = (bitsPerFrame - len);
    bits_in_buf = state->com.si_main_data_begin * 8;

    if (mp3_bitrate[header->id][header->layer-1][header->bitRate] != 320) {
        if (state->com.br_mode == MPAENC_ABR) {
          Ipp32s br_ind;

          bits_from_buffer = state->com.bytes_in_gbuf << 3;

          if (bits_from_buffer < state->com.slot_size * MAX_GBUF_COEF)
            mean_bits = C_0_85_Q15 * mean_bits >> 15;

          br_ind = mp3encLEBitrate(&state->com, (bitsPerFrame + bits_from_buffer) >> 3);

          bits_from_buffer =
            (state->com.slot_sizes[br_ind] - state->com.slot_size +
            state->com.si_main_data_begin) << 3;
        } else {
          if (bits_in_buf > (C_0_95_Q15 * max_bits_in_buf >> 15)) {
            bits_from_buffer = (C_0_95_Q15 * max_bits_in_buf >> 15);
            mean_bits += bits_in_buf - (C_0_95_Q15 * max_bits_in_buf >> 15);
          } else {
            bits_from_buffer = bits_in_buf;
          }
          mean_bits = C_0_85_Q15 * mean_bits >> 15;
        }

        mean_bits >>= stereo;

        com_additional_bits = 0;

        for (gr = 0; gr < 2; gr++) {
            for (ch = 0; ch < stereo; ch++) {
                Ipp32s pe = state->ipa_curr_frame_PE_st[gr][ch+ms] - 1000;
                if (pe > 32767) pe = 32767;

                additional_bits[gr][ch] = pe * C_0_6_Q15 >> 15;

                if (additional_bits[gr][ch] < 0) {
                    additional_bits[gr][ch] = 0;
                }
                if (state->com.si_blockType[gr][ch] != NORM_TYPE) {
                    if (additional_bits[gr][ch] * 2 < mean_bits)
                        additional_bits[gr][ch] = mean_bits >> 1;
                    else if (additional_bits[gr][ch] > (C_1_2_Q14 * mean_bits >> 14))
                        additional_bits[gr][ch] = (C_1_2_Q14 * mean_bits >> 14);
                } else {
                  Ipp32s max_add_bits;
                  if (state->com.br_mode == MPAENC_ABR)
                    max_add_bits = (C_0_5_Q15 * mean_bits >> 15);
                  else
                    max_add_bits = (C_0_2_Q15 * mean_bits >> 15);
                  if (additional_bits[gr][ch] > max_add_bits)
                    additional_bits[gr][ch] = max_add_bits;
                }

                com_additional_bits += additional_bits[gr][ch];
            }
        }

        if (com_additional_bits > bits_from_buffer && com_additional_bits)
            div = bits_from_buffer * 32768 / com_additional_bits;
        else
            div = 0;

        for (gr = 0; gr < 2; gr++) {
            for (ch = 0; ch < stereo; ch++) {
                if (com_additional_bits > bits_from_buffer) {
                    additional_bits[gr][ch] = additional_bits[gr][ch] * div >> 15;
                }

                state->com.max_bits[gr][ch] =
                    mean_bits + additional_bits[gr][ch];
            }
        }
        for (gr = 0; gr < 2; gr++) {
          if (state->com.stereo_mode == MPA_MS_STEREO) {
            Ipp32s add_bits;
            if (state->ms_dcoef[gr] == 0)
              add_bits = 0;
            else {
              ippsDiv_32s_Sfs(&state->ms_dcoef[gr], &state->ms_ncoef[gr], &add_bits, 1, -15);
              add_bits = 3 * state->com.max_bits[gr][1] * (add_bits - 16384) >> 16;
            }

            if (add_bits < 0) add_bits = 0;
            state->com.max_bits[gr][0] += add_bits;
            state->com.max_bits[gr][1] -= add_bits;
          }
        }
    } else {
        mean_bits += bits_in_buf;
        mean_bits >>= stereo;
        for (gr = 0; gr < 2; gr++)
            for (ch = 0; ch < stereo; ch++)
                state->com.max_bits[gr][ch] = mean_bits;
    }
    for (gr = 0; gr < 2; gr++)
      for (ch = 0; ch < stereo; ch++)
        if (state->com.max_bits[gr][ch] > 4095)
          state->com.max_bits[gr][ch] = 4095;
}

static Ipp16s scalefac_pow[] = {
  32767, 18658, 21247, 24196, 27554, 31379, 17867, 20347,
  23170, 26386, 30048, 17109, 19484, 22188, 25268, 28774
};

static Ipp16s scalefac_pow_shift[] = {
  0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3
};

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

static Ipp32s mp3ienc_AdjustSF(MP3Enc *state, Ipp32s *xmin_,
                               Ipp32s *distortion, Ipp32s *sfb_last, Ipp32s gr, Ipp32s ch,
                               Ipp32s mdct_scaleFactor, Ipp32s *pow34_scaleFact)
{
  Ipp32s i, sfb, width, pos, wnd;
  Ipp32u *sfb_long, *sfb_short;
  Ipp16s sf_tmp_16s;
  Ipp32s sf_tmp, scl;
  Ipp64s noise64;
  Ipp32s noise, xmin;
  Ipp16s ifqstep;
  Ipp32s dist_max, dist;
  Ipp32s ret_flag = 0;
  Ipp32s sf_glob;
  Ipp16s dist_max16;
  Ipp16s scaled_max;

  Ipp32s *mdct_line_abs = state->mdct_line_abs_int;
  Ipp16s *mdct_scaled_short = state->mdct_scaled_short;
  Ipp32s *mdct_rqnt = state->mdct_rqnt;
  Ipp16u scalefac_l_save[32];
  Ipp16u scalefac_s_save[12][3];

  sfb_long = mp3enc_sfBandIndex[state->com.header.id][state->com.header.samplingFreq].l;
  sfb_short = mp3enc_sfBandIndex[state->com.header.id][state->com.header.samplingFreq].s;

  if (state->com.si_sfScale[gr][ch] == 0) {
    ifqstep = 21247;
    scaled_max = 25266;
  } else {
    ifqstep = 27554;
    scaled_max = 19483;
  }

  scl = -2 - 2 * (Ipp16s)state->com.si_sfScale[gr][ch];
  sf_glob = state->com.si_globGain[gr][ch] - 106;

  if (state->com.si_blockType[gr][ch] != 2) {
    for (sfb = 0, pos = 0; sfb < state->com.sfb_l_max; sfb++, pos+=width) {
      scalefac_l_save[sfb] = state->com.scalefac_l[gr][ch][sfb];

      width = sfb_long[sfb+1] - sfb_long[sfb];
      noise = 0;

      if (state->com.si_preFlag[gr][ch] == 0)
        sf_tmp = scl * state->com.scalefac_l[gr][ch][sfb];
      else
        sf_tmp = scl * (state->com.scalefac_l[gr][ch][sfb]/* + pretab[sfb]*/);

      sf_tmp += sf_glob;

      if (sf_tmp == sfb_last[sfb])
        continue;
      sfb_last[sfb] = sf_tmp;

      sf_tmp_16s = (Ipp16s)sf_tmp;

      ippsPow43Scale_16s32s_Sf(state->com.quant_ix[gr][ch] + pos - sfb_long[sfb],
          mdct_rqnt + pos - sfb_long[sfb],
          &sf_tmp_16s, (const Ipp32s *)(sfb_long + sfb),0,1,1,0);

      noise64 = 0;

      for (i = 0; i < width; i++) {
        Ipp32s temp;
        temp = mdct_line_abs[pos+i] - mdct_rqnt[pos+i];
        noise64 += temp * (Ipp64s)temp;
      }
      if (2 * mdct_scaleFactor + 8 > 0)
        noise = (Ipp32s)(noise64 >> (2 * mdct_scaleFactor + 8));
      else
        noise = (Ipp32s)(noise64 << -(2 * mdct_scaleFactor + 8));

      xmin = xmin_[sfb];
      dist = 0;
      if (xmin > 0)
        ippsDiv_32s_Sfs(&xmin, &noise, &dist, 1, -22);

      distortion[sfb] = dist;
    }

    ippsMax_32s(distortion, state->com.sfb_l_max, &dist_max);

    ippsSqrt_32s16s_Sfs(&dist_max, &dist_max16, 1, 0);
    dist_max = dist_max16 << 11;

    if (dist_max >= (1 << 22)) {
      pos = 0;
      for (sfb = 0; sfb < state->com.sfb_l_max; sfb++) {
        Ipp16s sca_min16, sca_max16;
        Ipp32s sca_min, sca_max;
        width = sfb_long[sfb+1] - sfb_long[sfb];

        if (distortion[sfb] > dist_max)
        {
          Ipp32s s = state->com.scalefac_l[gr][ch][sfb];
          if (s < 3) {
            state->com.scalefac_l[gr][ch][sfb] = (Ipp16u)(s + 1);
            ippsMinMax_16s(mdct_scaled_short + pos, width, &sca_min16, &sca_max16);
            sca_min = -((Ipp32s)sca_min16);
            sca_max = sca_max16;

            if (sca_min > sca_max) {
              sca_max = sca_min - 1;
            }

            if (sca_max >= scaled_max) {
              *pow34_scaleFact = *pow34_scaleFact + 1;
              ippsRShiftC_16s_I(1, mdct_scaled_short, 576);
            }
            ippsMulC_16s_ISfs(ifqstep, mdct_scaled_short + pos, width, 14);

            ret_flag = 1;
          }
        }
        pos += width;
      }
    }
  } else {
    for (sfb = 0, pos = 0; sfb < state->com.sfb_s_max; sfb++) {
      width = sfb_short[sfb+1] - sfb_short[sfb];

      for (wnd = 0; wnd < 3; wnd++, pos += width) {
        scalefac_s_save[sfb][wnd] = state->com.scalefac_s[gr][ch][sfb][wnd];
        noise = 0;

        if (state->com.si_preFlag[gr][ch] == 0)
          sf_tmp = scl * state->com.scalefac_s[gr][ch][sfb][wnd];
        else
          sf_tmp = scl * (state->com.scalefac_s[gr][ch][sfb][wnd]/* + pretab[sfb]*/);

        sf_tmp += sf_glob;

        if (sf_tmp == sfb_last[sfb*3+wnd])
          continue;

        sfb_last[sfb*3+wnd] = sf_tmp;

        sf_tmp_16s = (Ipp16s)sf_tmp;

        ippsPow43Scale_16s32s_Sf(state->com.quant_ix[gr][ch] + pos - sfb_short[sfb],
          mdct_rqnt + pos - sfb_short[sfb],
          &sf_tmp_16s, (const Ipp32s *)(sfb_short + sfb),0,1,1,0);

        noise64 = 0;

        for (i = 0; i < width; i++) {
          Ipp32s temp;
          temp = mdct_line_abs[pos+i] - mdct_rqnt[pos+i];
          noise64 += temp * (Ipp64s)temp;
        }
        if (2 * mdct_scaleFactor + 8 > 0)
          noise = (Ipp32s)(noise64 >> (2 * mdct_scaleFactor + 8));
        else
          noise = (Ipp32s)(noise64 << -(2 * mdct_scaleFactor + 8));

        xmin = xmin_[sfb*3+wnd];
        dist = 0;
        if (xmin > 0)
          ippsDiv_32s_Sfs(&xmin, &noise, &dist, 1, -22);

        distortion[sfb*3+wnd] = dist;

      }
    }
    ippsMax_32s(distortion, state->com.sfb_s_max*3, &dist_max);

    ippsSqrt_32s16s_Sfs(&dist_max, &dist_max16, 1, 0);
    dist_max = dist_max16 << 11;

    if (dist_max >= (1 << 22))
    {
      pos = 0;
      for (sfb = 0; sfb < state->com.sfb_s_max; sfb++) {
        Ipp16s sca_min16, sca_max16;
        Ipp32s sca_min, sca_max;
        width = sfb_short[sfb+1] - sfb_short[sfb];
        for (wnd = 0; wnd < 3; wnd++) {
          if (distortion[sfb*3+wnd] > dist_max)
          {
            Ipp32s s = state->com.scalefac_s[gr][ch][sfb][wnd];
            if (s < 3) {

⌨️ 快捷键说明

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