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

📄 mp3enc_quantization_fp.c

📁 audio-video-codecs.rar语音编解码器
💻 C
📖 第 1 页 / 共 2 页
字号:

        sf_tmp_16s = (Ipp16s)sf_tmp;
        ippsCalcSF_16s32f(&sf_tmp_16s, 0, &tmp22, 1);

        ippsMulC_32f_I(tmp22, mdct_rqnt + pos, width);

        noise = 0.0f;

        for (i = 0; i < width; i++) {
          Ipp32f temp;
          temp = mdct_line_abs[pos+i] -  mdct_rqnt[pos+i];
          noise += temp * temp;
        }
        xmin = xmin_[sfb*3+wnd];
        dist = 0;
        if (xmin > 0) dist = noise / xmin;
        distortion[sfb*3+wnd] = dist;
      }
    }
    ippsMax_32f(distortion, state->com.sfb_s_max * 3, &dist_max);
    ippsSqrt_32f_I(&dist_max, 1);
    if (dist_max >= 1)
    {
      pos = 0;
      for (sfb = 0; sfb < state->com.sfb_s_max; sfb++) {
        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) {
              state->com.scalefac_s[gr][ch][sfb][wnd] = (Ipp16u)(s + 1);
              ippsMulC_32f_I(ifqstep, mdct_scaled + pos, width);
              ret_flag = 1;
            }
          }
          pos += width;
        }
      }
    }
  }

  mp3enc_quantcalcPart2Len(&state->com, gr, ch);
  if (state->com.si_part2Len[gr][ch] + state->com.si_part23Len[gr][ch] > (Ipp32u)state->com.max_bits[gr][ch]) {
    if (state->com.si_blockType[gr][ch] != 2) {
      for (sfb = 0; sfb < state->com.sfb_l_max; sfb++) {
        state->com.scalefac_l[gr][ch][sfb] = scalefac_l_save[sfb];
      }
    } else {
      for (sfb = 0; sfb < state->com.sfb_s_max; sfb++) {
        for (wnd = 0; wnd < 3; wnd++) {
          state->com.scalefac_s[gr][ch][sfb][wnd] = scalefac_s_save[sfb][wnd];
        }
      }
    }
    mp3enc_quantcalcPart2Len(&state->com, gr, ch);
    return 0;
  }

  return ret_flag;
}

void mp3enc_quantMainLoop(MP3Enc *state,
                          Ipp32s gr, Ipp32s ch,
                          Ipp32f* mdct_scaled)
{
  Ipp32f tmp_x_quant[LEN_MDCT_LINE];
  Ipp32f sf, temp;

  Ipp32s common_scalefactor;
  Ipp32s needed_bits;

  common_scalefactor = state->last_frame_common_scalefactor[ch];
  //    state->common_scalefactor_update[ch] = state->common_scalefactor_update[ch];

  if (common_scalefactor < state->start_common_scalefac)
    common_scalefactor = state->start_common_scalefac;

  if (common_scalefactor > state->finish_common_scalefac)
    common_scalefactor = state->finish_common_scalefac;

  for(;;) {
    sf = (Ipp32f)pow(2.0, -common_scalefactor * (3./16.));
    //sf = (Ipp32f)scalefac_pow[common_scalefactor + SF_OFFSET];
    temp = (Ipp32f)(MAGIC_NUMBER - 0.5)/sf;
    ippsAddC_32f( mdct_scaled, temp, tmp_x_quant, LEN_MDCT_LINE);
    ippsMulC_Low_32f16s(tmp_x_quant, sf, state->com.quant_ix[gr][ch], LEN_MDCT_LINE);

    needed_bits = mp3enc_quantCalcBits(&state->com, gr, ch);

    if (needed_bits == state->com.max_bits[gr][ch])
      break;

    if (needed_bits > state->com.max_bits[gr][ch]) {
      if (common_scalefactor == state->finish_common_scalefac)
        break;

      if (state->common_scalefactor_update[ch] < 0) {
        state->common_scalefactor_update[ch] = -state->common_scalefactor_update[ch];
      }
      state->common_scalefactor_update[ch] = (state->common_scalefactor_update[ch] + 1) >> 1;
    } else {
      if (common_scalefactor == state->start_common_scalefac)
        break;

      if (state->common_scalefactor_update[ch] == 1)
        break;

      if (state->common_scalefactor_update[ch] > 0) {
        state->common_scalefactor_update[ch] = -state->common_scalefactor_update[ch];
      }
      state->common_scalefactor_update[ch] >>= 1;
    }

    common_scalefactor += state->common_scalefactor_update[ch];

    if (common_scalefactor < state->start_common_scalefac)
      common_scalefactor = state->start_common_scalefac;

    if (common_scalefactor > state->finish_common_scalefac)
      common_scalefactor = state->finish_common_scalefac;

  };

  state->common_scalefactor_update[ch] =
    common_scalefactor - state->last_frame_common_scalefactor[ch];
  state->last_frame_common_scalefactor[ch] = common_scalefactor;

  if (state->common_scalefactor_update[ch] >= 0) {
    if (state->common_scalefactor_update[ch] <= 2)
      state->common_scalefactor_update[ch] = 2;
  } else {
    if (state->common_scalefactor_update[ch] >= -2)
      state->common_scalefactor_update[ch] = -2;
  }

  state->com.si_globGain[gr][ch] = (Ipp16s)(common_scalefactor + SF_OFFSET);
  state->com.si_part23Len[gr][ch] = needed_bits;
}

Ipp32s mp3enc_quantization_l3(MP3Enc *state)
{
    Ipp32s gr, ch, pos, wnd, sfb;
    Ipp32s iter, grnum;
    Ipp32u *sfb_long, *sfb_short;
    Ipp32s last_frame_common_scalefactor;
    IppMP3FrameHeader *header = &state->com.header;

    Ipp32f xmin[36], distortion[36], mdct_pwr;
    Ipp32s sfb_last[36];

    Ipp32f *mdct_line_abs = state->mdct_line_abs;
    Ipp32f *mdct_scaled = state->mdct_scaled;

    grnum = state->com.grnum;

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

    mp3enc_quantCalcAvailableBits(state);

    for (gr = 0; gr < grnum; gr++) {
        for (ch = 0; ch < state->com.stereo; ch++) {
            Ipp32f max_mdct_line;
            Ipp32f tmp_start_common_scalefac;
            Ipp32f tmp_finish_common_scalefac;

            mp3enc_quantIterReset(&state->com, gr, ch);

            ippsAbs_32f(state->mdct_out[gr][ch], mdct_line_abs, LEN_MDCT_LINE);
            ippsPow34_32f(mdct_line_abs, mdct_scaled, LEN_MDCT_LINE);

            if (state->com.ns_mode) {
              if (state->com.si_blockType[gr][ch] != 2) {
                pos = 0;
                for (sfb = 0; sfb < state->com.sfb_l_max; sfb++) {
                  Ipp32s width = sfb_long[sfb+1] - sfb_long[sfb];
                  ippsDotProd_32f(&mdct_line_abs[pos], &mdct_line_abs[pos], width, &mdct_pwr);
                  xmin[sfb] = mdct_pwr * state->pa_ratio_l[gr][ch][sfb];
                  sfb_last[sfb] = 10000;
                  pos += width;
                }
              } else {
                pos = 0;
                for (sfb = 0; sfb < state->com.sfb_s_max; sfb++) {
                  Ipp32s width = sfb_short[sfb+1] - sfb_short[sfb];
                  for (wnd = 0; wnd < 3; wnd++) {
                    ippsDotProd_32f(&mdct_line_abs[pos], &mdct_line_abs[pos], width, &mdct_pwr);
                    xmin[sfb*3+wnd] = mdct_pwr * state->pa_ratio_s[gr][ch][wnd][sfb];
                    sfb_last[sfb*3+wnd] = 10000;
                    pos += width;
                  }
                }
              }
            }

            iter = 0;
            last_frame_common_scalefactor = state->last_frame_common_scalefactor[ch];
            do {
              state->start_common_scalefac = -SF_OFFSET;
              state->finish_common_scalefac = -SF_OFFSET;
              max_mdct_line = 0;
              ippsMax_32f(mdct_scaled, LEN_MDCT_LINE, &max_mdct_line);

              if (max_mdct_line > 0) {
                tmp_start_common_scalefac = (Ipp32f)log(max_mdct_line/(MAX_QUANT))/(Ipp32f)log(2.0);
                tmp_start_common_scalefac *= (Ipp32f)16/(Ipp32f)3;
                state->start_common_scalefac = (Ipp32s)floor((Ipp64f)tmp_start_common_scalefac);

                if (state->start_common_scalefac < tmp_start_common_scalefac)
                  state->start_common_scalefac++;

                tmp_finish_common_scalefac = (Ipp32f)log(max_mdct_line/(1 - MAGIC_NUMBER))/(Ipp32f)log(2.0);
                tmp_finish_common_scalefac *= (Ipp32f)16/(Ipp32f)3;
                state->finish_common_scalefac = (Ipp32s)floor((Ipp64f)tmp_finish_common_scalefac);

                if (state->finish_common_scalefac < tmp_finish_common_scalefac)
                  state->finish_common_scalefac++;

                state->finish_common_scalefac++;

                if (state->start_common_scalefac > 255 - SF_OFFSET) {
                  state->start_common_scalefac = 255 - SF_OFFSET;
                } else if (state->start_common_scalefac < -SF_OFFSET) {
                  state->start_common_scalefac = -SF_OFFSET;
                }

                if (state->finish_common_scalefac > 255 - SF_OFFSET) {
                  state->finish_common_scalefac = 255 - SF_OFFSET;
                } else if (state->finish_common_scalefac < -SF_OFFSET) {
                  state->finish_common_scalefac = -SF_OFFSET;
                }
              }

              mp3enc_quantMainLoop(state,
                gr, ch, mdct_scaled);

              state->common_scalefactor_update[ch] = 2;

              if (state->com.ns_mode == 0)
                break;
            } while(iter++ < QUANT_ITER_NUM &&
              mp3enc_AdjustSF(state, xmin, distortion, sfb_last, gr, ch));

            state->common_scalefactor_update[ch] = state->com.si_globGain[gr][ch] - SF_OFFSET -
              last_frame_common_scalefactor;

            if (state->common_scalefactor_update[ch] >= 0) {
              if (state->common_scalefactor_update[ch] <= 2)
                state->common_scalefactor_update[ch] = 2;
            } else {
              if (state->common_scalefactor_update[ch] >= -2)
                state->common_scalefactor_update[ch] = -2;
            }
            state->com.frameBits += state->com.si_part2Len[gr][ch];
            state->com.frameBits += state->com.si_part23Len[gr][ch];
        }
    }
    if (state->com.br_mode == MPAENC_ABR) {
      state->com.header.bitRate =
        mp3encGEBitrate(&state->com, ((state->com.frameBits + 7) >> 3) - state->com.si_main_data_begin);
    }
    return 1;
}

⌨️ 快捷键说明

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