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

📄 aac_enc_elements.c

📁 audio-video-codecs.rar语音编解码器
💻 C
📖 第 1 页 / 共 2 页
字号:
  for (i = 0; i < num_scale_factor; i++) {
    switch(pStream->sfb_cb[i]) {
    case ZERO_HCB:
      break;
    case INTENSITY_HCB:
      break;
    case INTENSITY_HCB2:
      break;
    default:
      index = pStream->scale_factors[i] - last_sf + SF_MID;
      len  = sf_huff_codebook[2*index+0];
      if (writing) {
        code = sf_huff_codebook[2*index+1];
        PUT_BITS(pBS,code,len);
      } else {
        count_bits += len;
      }
      last_sf = pStream->scale_factors[i];
      break;
    }
  }
  return count_bits;
}

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

void enc_spectral_data(sEnc_individual_channel_stream* pStream,
                       sBitsreamBuffer* pBS)
{
  IppsVLCEncodeSpec_32s* pVLCEncSpec;
  Ipp8u  *pDst;
  Ipp16s tmp_src[512], *q;
  Ipp32s* sfb_offset;
  Ipp32s i, j;
  Ipp32s win;
  Ipp32s sect_cb;
  Ipp32s sect_counter;
  Ipp32s sect_end;
  Ipp32s data_begin;
  Ipp32s data_end;
  Ipp32s shift;
  Ipp32s offset;
  Ipp32s bitoffset;

  sfb_offset = pStream->sfb_offset;

  pDst = (Ipp8u*)pBS->pCurrent_dword + ((32 - pBS->nBit_offset) >> 3);
  bitoffset = (32 - pBS->nBit_offset) & 0x7;

  SAVE_BITSTREAM(pBS)

  sect_counter = 0;

  for (win = 0; win < pStream->num_window_groups; win++) {

    data_begin = sfb_offset[0];
    sect_end = 0;

    for (i = 0; i < pStream->sect_num[win]; i++) {
      sect_end += pStream->sect_len[sect_counter];
      data_end  = sfb_offset[sect_end];
      sect_cb   = pStream->sect_cb[sect_counter];

      shift = vlcEncShifts[sect_cb];
      offset = vlcEncOffsets[sect_cb];

      pVLCEncSpec = (IppsVLCEncodeSpec_32s*)pStream->pHuffTables[sect_cb];

      switch (vlcEncTypes[sect_cb]) {
      case 0: /* 4 tuples */
        q = &pStream->x_quant[data_begin];
        for (j = 0; j < (data_end-data_begin) >> 2; j++) {
          tmp_src[j] = (Ipp16s)((q[0] << (3*shift)) + ((q[1] + offset) << (2*shift)) +
                        ((q[2] + offset) << shift) + (q[3] + offset));
          q += 4;
        }

        ippsVLCEncodeBlock_16s1u(tmp_src, (data_end-data_begin) >> 2, &pDst,
                                 &bitoffset, pVLCEncSpec);
        break;
      case 1: /* 2 tuples */
        q = &pStream->x_quant[data_begin];
        for (j = 0; j < (data_end-data_begin) >> 1; j++) {
          tmp_src[j] = (Ipp16s)((q[0] << shift) + (q[1] + offset));
          q += 2;
        }

        ippsVLCEncodeBlock_16s1u(tmp_src, (data_end-data_begin) >> 1, &pDst,
                                 &bitoffset, pVLCEncSpec);
        break;
      case 2: /* esc */
        ippsVLCEncodeEscBlock_AAC_16s1u(&pStream->x_quant[data_begin],
                                        (data_end-data_begin), &pDst,
                                        &bitoffset, pVLCEncSpec);
      }

      data_begin = data_end;
      sect_counter++;
    }

    sfb_offset += pStream->max_sfb;
  }

  pBS->pCurrent_dword = (Ipp32u*)(pDst - ((Ipp32s)(pDst) & 3));
  pBS->nBit_offset = 32 - ((pDst - (Ipp8u*)pBS->pCurrent_dword) << 3)-
                     bitoffset;
  LOAD_DWORD(pBS)
}

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

Ipp32s enc_individual_channel_stream(sEnc_individual_channel_stream* pStream,
                                     Ipp32s common_window,
                                     Ipp32s scale_flag,
                                     sBitsreamBuffer* pBS,
                                     Ipp32s writing)
{
  Ipp32s sb;
  Ipp32s count_bits;

  if (writing) {
    Ipp32s num_scale_factor;

    num_scale_factor = pStream->num_window_groups * pStream->max_sfb;

    for (sb = 0; sb < num_scale_factor; sb++) {
      if (pStream->sfb_cb[sb] != 0) {
        pStream->global_gain = pStream->scale_factors[sb];
        break;
      }
    }

    PUT_BITS(pBS,pStream->global_gain,8);
  }

  count_bits = 8;

  if (!common_window && !scale_flag) {
    count_bits += enc_ics_info(pStream, pBS, writing);
  }

  if (writing) {
    enc_section_data(pStream, pBS);
    enc_scale_factor_data(pStream, pBS, 1);
  }

  if (!scale_flag) {
    if (writing) {
      PUT_BITS(pBS,0,1); /// Pulse data isn't supported.
      PUT_BITS(pBS,0,1); /// TNS data isn't supported.
      PUT_BITS(pBS,0,1); /// Gain control data isn't supported.
    }
    count_bits += 3;
  }

  if (writing) {
    enc_spectral_data(pStream, pBS);
  }

  return count_bits;
}

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

Ipp32s enc_single_channel_element(sEnc_single_channel_element* pElement,
                                  Ipp32s element_instance_tag,
                                  sBitsreamBuffer* pBS,
                                  Ipp32s writing,
                                  sCrcSaveTable *CrcSaveTable)
{
  Ipp32s count_bits;
  Ipp32s decodedBits0 = 0, decodedBits2 = 0;

  if (writing) {
    CrcSaveTable[0].crc_ptr = pBS->pCurrent_dword;
    CrcSaveTable[0].crc_offset = pBS->nBit_offset;
    GET_BITS_COUNT(pBS, decodedBits0)
  }

  if (writing) {
    PUT_BITS(pBS, element_instance_tag,4);
  }

  count_bits = 4;

  count_bits += enc_individual_channel_stream(pElement->p_individual_channel_stream,
                                              0, 0, pBS, writing);

  if (writing) {
    Ipp32s len;

    GET_BITS_COUNT(pBS, decodedBits2)

    len = decodedBits2 - decodedBits0;
    if (len > 192) len = 192;

    CrcSaveTable[0].crc_len = len;
    CrcSaveTable[0].crc_zero_len = 192 - len;
  }

  return count_bits;
}

extern Ipp32s ubit[];

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

Ipp32s enc_channel_pair_element(sEnc_channel_pair_element* pElement,
                                Ipp32s element_instance_tag,
                                sBitsreamBuffer* pBS,
                                Ipp32s writing,
                                sCrcSaveTable *CrcSaveTable)
{
  Ipp32s common_window = pElement->common_window;
  Ipp32s count_bits;
  Ipp32s decodedBits0 = 0, decodedBits1 = 0, decodedBits2 = 0;

  if (writing) {
    CrcSaveTable[0].crc_ptr = pBS->pCurrent_dword;
    CrcSaveTable[0].crc_offset = pBS->nBit_offset;
    GET_BITS_COUNT(pBS, decodedBits0)
  }

  if (writing) {
    PUT_BITS(pBS,element_instance_tag,4);
    PUT_BITS(pBS,common_window,1);
  }

  count_bits = 5;

  if (common_window) {
    count_bits += enc_ics_info(pElement->p_individual_channel_stream_0,
                               pBS, writing);

    if ((pElement->p_individual_channel_stream_0->audioObjectType == AOT_AAC_LTP) &&
        (EIGHT_SHORT_SEQUENCE !=
        pElement->p_individual_channel_stream_0->windows_sequence)) {

      if (pElement->p_individual_channel_stream_0->predictor_data_present) {
        if (writing) {
          PUT_BITS(pBS, pElement->p_individual_channel_stream_1->ltp_data_present, 1)
        }
        count_bits++;

        if (pElement->p_individual_channel_stream_1->ltp_data_present) {
          count_bits += enc_ltp_data(pElement->p_individual_channel_stream_1,
                                     pBS, writing);
        }
      }
    }

    if (writing) {
      PUT_BITS(pBS, pElement->ms_mask_present, 2);

      if (pElement->ms_mask_present == 1) {
        Ipp32s max_sfb = pElement->p_individual_channel_stream_0[0].max_sfb;
        Ipp32s num_window_groups = pElement->p_individual_channel_stream_0[0].num_window_groups;
        Ipp32s sfb;

        for (sfb = 0; sfb < num_window_groups*max_sfb; sfb++) {
          PUT_BITS(pBS, pElement->ms_used[sfb], 1)
        }
        count_bits += num_window_groups*max_sfb;
      }
    }

    count_bits += 2;
  }

  count_bits += enc_individual_channel_stream(pElement->p_individual_channel_stream_0,
                                              common_window, 0, pBS, writing);

  if (writing) {
    CrcSaveTable[1].crc_ptr = pBS->pCurrent_dword;
    CrcSaveTable[1].crc_offset = pBS->nBit_offset;
    GET_BITS_COUNT(pBS, decodedBits1)
  }

  count_bits += enc_individual_channel_stream(pElement->p_individual_channel_stream_1,
                                              common_window, 0, pBS, writing);

  if (writing) {
    Ipp32s len;

    GET_BITS_COUNT(pBS, decodedBits2)

    len = decodedBits2 - decodedBits0;
    if (len > 192) len = 192;
    CrcSaveTable[0].crc_len = len;
    CrcSaveTable[0].crc_zero_len = 192 - len;

    len = decodedBits2 - decodedBits1;
    if (len > 128) len = 128;
    CrcSaveTable[1].crc_len = len;
    CrcSaveTable[1].crc_zero_len = 128 - len;
  }

  return count_bits;
}

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

Ipp32s enc_ltp_data(sEnc_individual_channel_stream* pStream,
                    sBitsreamBuffer* pBS,
                    Ipp32s writing)
{
  Ipp32s  i;
  Ipp32s  pred_max_sfb;

  pred_max_sfb =
    MAX_LTP_SFB_LONG < pStream->max_sfb ? MAX_LTP_SFB_LONG : pStream->max_sfb;

  if (writing) {

    PUT_BITS(pBS, pStream->ltp_lag, 11)
    PUT_BITS(pBS, pStream->ltp_coef, 3)

    for (i = 0; i < pred_max_sfb; i++) {
      PUT_BITS(pBS, pStream->ltp_long_used[i], 1)
    }
  }

  return (14 + pred_max_sfb);
}

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

#if 0

void tns_data(sEnc_individual_channel_stream* pStream,
              sBitsreamBuffer* pBS)
{
  Ipp32s w;
  Ipp32s filt;
  Ipp32s i;
  Ipp32s coef_len;
  sTns_data* pData;

  pData = &pStream->tns_data;

  for (w = 0; w < pData->w_number; w++)
  {
    PUT_BITS(pBS,pData->n_filt[w],pData->is_window_long ? 2 : 1);
    if (pData->n_filt[w] == 0) continue;
    PUT_BITS(pBS,pData->coef_res[w],1);

    for(filt = 0; filt < pData->n_filt[w]; filt ++)
    {
      PUT_BITS(pBS,pData->length[w][filt],pData->is_window_long ? 6:4);
      PUT_BITS(pBS,pData->order[w][filt],pData->is_window_long ? 5:3);

      if (pData->order[w][filt] == 0) continue;

      PUT_BITS(pBS,pData->direction[w][filt],1);
      PUT_BITS(pBS,pData->coef_compress[w][filt],1);

      coef_len = 3 + pData->coef_res[w] - pData->coef_compress[w][filt];

      for(i = 0; i < pData->order[w][filt]; i++)
      {
        PUT_BITS(pBS,pData->coef[w][filt][i],coef_len);
      }
    }
  }
}

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

#endif

Ipp32u sf_huff_codebook[] = {

 /* len, code*/
 18, 0x3FFE8, 18, 0x3FFE6, 18, 0x3FFE7, 18, 0x3FFE5,
 19, 0x7FFF5, 19, 0x7FFF1, 19, 0x7FFED, 19, 0x7FFF6,
 19, 0x7FFEE, 19, 0x7FFEF, 19, 0x7FFF0, 19, 0x7FFFC,
 19, 0x7FFFD, 19, 0x7FFFF, 19, 0x7FFFE, 19, 0x7FFF7,
 19, 0x7FFF8, 19, 0x7FFFB, 19, 0x7FFF9, 18, 0x3FFE4,
 19, 0x7FFFA, 18, 0x3FFE3, 17, 0x1FFEF, 17, 0x1FFF0,
 16, 0x0FFF5, 17, 0x1FFEE, 16, 0x0FFF2, 16, 0x0FFF3,
 16, 0x0FFF4, 16, 0x0FFF1, 15, 0x07FF6, 15, 0x07FF7,
 14, 0x03FF9, 14, 0x03FF5, 14, 0x03FF7, 14, 0x03FF3,
 14, 0x03FF6, 14, 0x03FF2, 13, 0x01FF7, 13, 0x01FF5,
 12, 0x00FF9, 12, 0x00FF7, 12, 0x00FF6, 11, 0x007F9,
 12, 0x00FF4, 11, 0x007F8, 10, 0x003F9, 10, 0x003F7,
 10, 0x003F5,  9, 0x001F8,  9, 0x001F7,  8, 0x000FA,
  8, 0x000F8,  8, 0x000F6,  7, 0x00079,  6, 0x0003A,
  6, 0x00038,  5, 0x0001A,  4, 0x0000B,  3, 0x00004,
  1, 0x00000,  4, 0x0000A,  4, 0x0000C,  5, 0x0001B,
  6, 0x00039,  6, 0x0003B,  7, 0x00078,  7, 0x0007A,
  8, 0x000F7,  8, 0x000F9,  9, 0x001F6,  9, 0x001F9,
 10, 0x003F4, 10, 0x003F6, 10, 0x003F8, 11, 0x007F5,
 11, 0x007F4, 11, 0x007F6, 11, 0x007F7, 12, 0x00FF5,
 12, 0x00FF8, 13, 0x01FF4, 13, 0x01FF6, 13, 0x01FF8,
 14, 0x03FF8, 14, 0x03FF4, 16, 0x0FFF0, 15, 0x07FF4,
 16, 0x0FFF6, 15, 0x07FF5, 18, 0x3FFE2, 19, 0x7FFD9,
 19, 0x7FFDA, 19, 0x7FFDB, 19, 0x7FFDC, 19, 0x7FFDD,
 19, 0x7FFDE, 19, 0x7FFD8, 19, 0x7FFD2, 19, 0x7FFD3,
 19, 0x7FFD4, 19, 0x7FFD5, 19, 0x7FFD6, 19, 0x7FFF2,
 19, 0x7FFDF, 19, 0x7FFE7, 19, 0x7FFE8, 19, 0x7FFE9,
 19, 0x7FFEA, 19, 0x7FFEB, 19, 0x7FFE6, 19, 0x7FFE0,
 19, 0x7FFE1, 19, 0x7FFE2, 19, 0x7FFE3, 19, 0x7FFE4,
 19, 0x7FFE5, 19, 0x7FFD7, 19, 0x7FFEC, 19, 0x7FFF4,
 19, 0x7FFF3
};

⌨️ 快捷键说明

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