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

📄 mp3enc_api_fp.c

📁 audio-video-codecs.rar语音编解码器
💻 C
📖 第 1 页 / 共 2 页
字号:
        } else {
          chnum += 2;
          stereo = 2;
        }
      } else if (channel_mask & MP3_CHANNEL_CENTER) {
        chnum ++;
        stereo = 1;
      }

      if ((channel_mask & MP3_CHANNEL_SURROUND_STEREO) &&
        (channel_mask & MP3_CHANNEL_SURROUND_MONO)) {
        vm_debug_trace(VM_DEBUG_ERROR, VM_STRING("Unsupported multichannel configuration (0)\n"));
        return MP3_FAILED_TO_INITIALIZE;
      } else if (channel_mask & MP3_CHANNEL_SURROUND_STEREO) {
        chnum += 2;
        mc_surround = 2;
      } else if (channel_mask & MP3_CHANNEL_SURROUND_MONO) {
        chnum ++;
        mc_surround = 1;
      } else if (channel_mask & MP3_CHANNEL_SURROUND_STEREO_P2) {
        chnum += 2;
        mc_surround = 3;
      }

      if (channel_mask & MP3_CHANNEL_LOW_FREQUENCY) {
        if (!mc_surround && !mc_center) {
          vm_debug_trace(VM_DEBUG_ERROR, VM_STRING("Unsupported multichannel configuration (1)\n"));
          return MP3_FAILED_TO_INITIALIZE;
        }
        mc_lfe = 1;
      }

      mc_conf = (stereo + mc_center) * 100;
      if (mc_surround < 3)
        mc_conf += mc_surround * 10;
      else
        mc_conf += 2;

      switch (mc_conf) {
        case 320:
        case 302:
        case 310:
        case 220:
        case 202:
        case 300:
        case 210:
        case 200:
        case 100:
          vm_debug_trace2(VM_DEBUG_INFO, VM_STRING("Multichannel configuration: %d, lfe = %d\n"),
            mc_conf, mc_lfe);
          break;
        default:
          vm_debug_trace(VM_DEBUG_ERROR, VM_STRING("Unsupported multichannel configuration (2)\n"));
          return MP3_FAILED_TO_INITIALIZE;
      }
      mc_channel = chnum - stereo;
    }

    if (mc_channel)
      force_mpeg1 = 1;

    if (force_mpeg1 && id == 0) {
      id = 1;
      upsample++;
    }

    if (stereo != 1 && stereo != 2)
      return MP3_UNSUPPORTED;
    if(!mp3enc_checkBitRate(id, layer, stereo + mc_channel, bitrate, &br)) {
      return MP3_FAILED_TO_INITIALIZE;
    }

    if ((layer == 1 || (layer == 2 && mc_channel)) &&
      (stereo_mode == MPA_JOINT_STEREO || stereo_mode == MPA_MS_STEREO)) {
        stereo_mode = MPA_LR_STEREO;
    }

    ts = sizeof(MP3Enc);
    size += ts;
    if (state)
      mem = (Ipp8u *)state + ts;

    if(state) {
      ippsZero_8u((Ipp8u *)state, sizeof(MP3Enc));

      state->com.mc_header.center = mc_center;
      state->com.mc_header.surround = mc_surround;
      state->com.mc_header.lfe = mc_lfe;
      state->com.mc_channel_conf = mc_conf;
      state->com.mc_channel_mask = channel_mask;
      state->com.mc_channel = mc_channel;
      state->com.mc_header.dematrix_procedure = dematrix_procedure;
      state->com.mc_lfe_filter_off = mc_lfe_filter_off;

      state->com.upsample = upsample;
      state->com.header.samplingFreq = freq;
      state->com.header.bitRate = br;

      state->com.header.id = id;
      state->com.header.layer = layer;   // layer 3
      state->com.header.protectionBit = 1; // enable CRC

      if (br == 1)
        br_mode = MPAENC_CBR; // use CBR for minimal bitrate

      state->com.br_mode = br_mode;
      state->com.stereo = stereo;
      mp3encGetFrameSize(&(state->com.frameSize), id ,layer, state->com.upsample);

      state->com.si_main_data_begin = 0;
      state->com.main_data_ptr = 0;
      state->com.resr_bytes = 0;
      state->com.resr_mod_slot = 0;

      state->com.quant_mode_fast = 0;

      state->com.framesNum = 0;

      state->com.ns_mode = ns_mode;
      state->com.stereo_mode =
        state->com.stereo_mode_param = stereo_mode;

      if (stereo == 2 && state->com.stereo_mode_param == MPA_JOINT_STEREO)
        state->com.jschannel = 1;
      else
        state->com.jschannel = 0;

      samp_rate_per_ch = (Ipp32f)(mp3_bitrate[state->com.header.id]
        [state->com.header.layer-1][state->com.header.bitRate] * 1000 / stereo);
      ippsPow34_32f(&samp_rate_per_ch, &cutoff_frequency, 1);
      cutoff_frequency *= 8.0;

      state->com.lowpass_maxline = (Ipp32s)(32 * cutoff_frequency /
          (Ipp32f)(mp3_frequency[state->com.header.id][state->com.header.samplingFreq]));
      if (state->com.lowpass_maxline > 32)
          state->com.lowpass_maxline = 32;

      ippsZero_32f(&state->input_buffer[0][0],2*(1152+480));

      mp3encCalcSlotSize(state);
    }

    ippsAnalysisFilterGetSize_PQMF_MP3_32f(&ts);
    size += ts * NUM_CHANNELS;

    if (mem) {
      state->pqmf_size = ts;
      state->pqmf_mem = mem;
      for (j = 0; j < NUM_CHANNELS; j++) {
        ippsAnalysisFilterInit_PQMF_MP3_32f(&state->pPQMFSpec[j],
          state->pqmf_mem + state->pqmf_size * j);
      }
      mem += ts * NUM_CHANNELS;
    }

    mp3enc_psychoacousticInit(state, mem, &ts);
    size += ts;
    if (mem) mem += ts;

    ts = 0;
    switch(layer) {
      case 1:
        mp3encInit_layer1(state);
        break;
      case 2:
        mp3encInit_layer2(state);
        break;
      case 3:
        mp3encInit_layer3(state, ns_mode, mem, &ts);
        break;
      default:
        break;
    }

    size += ts;
    if (mem) mem += ts;

    if(size_all)
      *size_all = size;

    return MP3_OK;
}

static void mp3encClose_layer3(MP3Enc *state)
{
  if(state->pMDCTSpec36)
    ippsFree(state->pMDCTSpec36);
  if(state->pMDCTSpec12)
    ippsFree(state->pMDCTSpec12);
  state->com.si_private_bits = 0;
}

MP3Status mp3encClose(MP3Enc *state)
{
  if (NULL == state){
    return MP3_OK;
  }

  switch(state->com.header.layer) {
      case 1:
        break;
      case 2:
        break;
      case 3:
        mp3encClose_layer3(state);
        break;
      default:
        break;
  }

  return MP3_OK;
}

MP3Status mp3encGetFrame(Ipp16s *inPointer, Ipp32s *encodedBytes,
                         Ipp8u *outPointer, MP3Enc *state)
{
    VM_ALIGN16_DECL(Ipp16s) buffer[NUM_CHANNELS+LFE_CHANNEL][1152];
    Ipp16s  *buff[NUM_CHANNELS+LFE_CHANNEL];

    Ipp32s  ch, gr, i;
    Ipp32s  stereo;

    Ipp32f  *input_data[NUM_CHANNELS];
    Ipp32f  tbuf[576];
    Ipp32s  frame_size, upsample;

    if (!state || !encodedBytes || !inPointer || !outPointer)
      return MP3_NULL_PTR;

    for (i = 0; i < NUM_CHANNELS+LFE_CHANNEL; i++)
      buff[i] = buffer[i];

    for (i = 0; i < NUM_CHANNELS; i++)
      input_data[i] = state->input_buffer[i];

    stereo = state->com.stereo;// + state->com.mc_channel + state->com.mc_header.lfe;

    ippsDeinterleave_16s(inPointer, stereo + state->com.mc_channel +
      state->com.mc_header.lfe, state->com.frameSize, buff);

    switch (state->com.header.layer) {
      case 1:
        for (ch = 0; ch < stereo; ch++) {
          Ipp32s  upsample = state->com.upsample;
          ippsMove_32f(&(state->input_buffer[ch][480]), &(state->input_buffer[ch][0]), 576);
          ippsCopy_32f(&(state->input_buffer[ch][960]), &(state->input_buffer[ch][576]), 64);
          if (upsample) {
            Ipp32s len = 384, phase = 0;
            ippsConvert_16s32f(buff[ch], tbuf, 384 >> upsample);
            ippsSampleUp_32f(tbuf, 384 >> upsample, &state->input_buffer[ch][576+64], &len, 1 << upsample, &phase);
          } else {
            ippsConvert_16s32f(buff[ch], &state->input_buffer[ch][576+64], 384);
          }
        }
        mp3enc_psychoacoustic_l1(state, input_data);

        for (ch = 0; ch < stereo; ch++) {
          for (i = 0; i < 12; i++) {
            ippsAnalysisFilter_PQMF_MP3_32f(&state->input_buffer[ch][576] + i * 32,
              &(state->fbout_data[0][ch][0][0]) + i * 32,
              state->pPQMFSpec[ch], 1);
          }
        }

        mp3enc_quantization_l1(state);
        *encodedBytes = mp3enc_formatBitstream_l12(&state->com, outPointer);
        break;
      case 2:
        state->com.jsbound = state->com.sblimit;
        upsample = state->com.upsample;

        for (ch = 0; ch < stereo + state->com.mc_channel + state->com.mc_header.lfe; ch++) {
          ippsCopy_32f(&(state->input_buffer[ch][1152]), &(state->input_buffer[ch][0]), 480);
          if (upsample) {
            Ipp32s len = 1152, phase = 0;
            ippsConvert_16s32f(buff[ch], tbuf, 1152 >> upsample);
            ippsSampleUp_32f(tbuf, 1152 >> upsample, &state->input_buffer[ch][480], &len, 1 << upsample, &phase);
          } else {
            ippsConvert_16s32f(buff[ch], &state->input_buffer[ch][480], 1152);
          }
        }
        if (state->com.mc_header.lfe) {
          if (state->com.mc_lfe_filter_off) {
            Ipp32f *ptr = &state->input_buffer[stereo + state->com.mc_channel][480];
            for (i = 0; i < 12; i++) {
              state->mc_lfe_buf[i] = *ptr * (1.0f / 32768.0f);
              ptr += 96;
            }
          } else {
            Ipp32f lfe_val;
            Ipp32f *ptr = &state->input_buffer[stereo + state->com.mc_channel][0];
            for (i = 0; i < 12; i++) {
              ippsDotProd_32f(ptr, mp3_lfe_filter, 480, &lfe_val);
              state->mc_lfe_buf[i] = lfe_val * (1.0f / 32768.0f);
              ptr += 96;
            }
          }
        }

        for (ch = 0; ch < stereo + state->com.mc_channel; ch++) {
          for (i = 0; i < 36; i++) {
            ippsAnalysisFilter_PQMF_MP3_32f(&state->input_buffer[ch][480] + i * 32,
              &(state->fbout_data[ch][0][0][0]) + i * 32,
              state->pPQMFSpec[ch], 1);
          }
        }

        if (state->com.mc_channel) {
          mp3enc_mc_normalizing(state);
          mp3enc_mc_matricing(state);
        }

        mp3enc_psychoacoustic_l2(state, input_data);

        mp3enc_quantization_l2(state);
        *encodedBytes = mp3enc_formatBitstream_l12(&state->com, outPointer);
        break;
      case 3:
        frame_size = state->com.frameSize;
        for (ch = 0; ch < stereo; ch++) {
            Ipp32s  upsample = state->com.upsample;
            ippsCopy_32f(&(state->input_buffer[ch][frame_size]), &(state->input_buffer[ch][0]), 448);
            if (upsample) {
              Ipp32s len = frame_size, phase = 0;
              ippsConvert_16s32f(buff[ch], tbuf, frame_size >> upsample);
              ippsSampleUp_32f(tbuf, frame_size >> upsample, &state->input_buffer[ch][448],
                &len, 1 << upsample, &phase);
            } else {
              ippsConvert_16s32f(buff[ch], &state->input_buffer[ch][448], frame_size);
            }
        }

        mp3enc_psychoacoustic_l3(state, input_data);

        // filterbank
        for (gr = 0; gr < state->com.grnum; gr++)
          for (ch = 0; ch < stereo; ch++) {
            for (i = 0; i < 18; i++) {
              ippsAnalysisFilter_PQMF_MP3_32f(&state->input_buffer[ch][129 + gr * 576] + i * 32,
                (Ipp32f *)&((*(state->fbout[gr+1]))[ch][0][0]) + i * 32,
                state->pPQMFSpec[ch], 1);
            }
          }
        mp3enc_mdctBlock(state);

        mp3enc_quantization_l3(state);

        *encodedBytes = mp3enc_formatBitstream_l3(&state->com, (Ipp32s (*)[2][576])state->mdct_out, outPointer);
        break;
    }

    state->com.framesNum++;

    return MP3_OK;
}

MP3Status mp3encGetMpegId(MP3Enc *state, Ipp32s *id)
{
  *id = state->com.header.id;
  return MP3_OK;
}

MP3Status mp3encGetUpsample(MP3Enc *state, Ipp32s *upsample)
{
  *upsample = state->com.upsample;
  return MP3_OK;
}

MP3Status mp3encGetSlotSize(MP3Enc *state, Ipp32s *slot_size)
{
  *slot_size = state->com.slot_size;
  return MP3_OK;
}

⌨️ 快捷键说明

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