📄 mp3enc.c
字号:
/******************************************************************************
// 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 Intel Corporation. All Rights Reserved.
//
// Description:
// Intel(R) Integrated Performance Primitives Sample Code MP3 Encoder
//
// Function List:
// encode_mp3()
******************************************************************************/
#include "sampmp3.h"
/******************************************************************************
// Name: encode_mp3
// Description: Frame-based MP3 encode.
// Input Arguments : sound - pointer to input sound structure.
// enc_state - pointer to encoder state structure.
// Output Arguments: stream_buf - pointer to output stream structure.
// enc_state - pointer to updated encoder state structure.
// Returns:
// SAMPLE_STATUS_BADARG_ERR - Bad argument
// SAMPLE_STATUS_ERR - Error when encode one frame
// SAMPLE_STATUS_NOERR - Encode one frame successfully
******************************************************************************/
sample_status encode_mp3(sample_sound *sound,
sample_bitstream *stream_buf,
mp3_enc_state *enc_state)
{
int i;
int ch, gr;
int subband_num;
int totalbuf_size;
int channel_num,granule_num;
IppMP3EncPsychoAcousticModel2State *psy_state;
IppMP3PsychoacousticModelTwoAnalysis *psy_info;
IppMP3SideInfo *side_info, *current_side_info;
IppMP3FrameHeader *frame_header;
IppMP3BitReservoir *resv;
Ipp32s *work_buf;
Ipp16s *pcm_buf;
int *is_sfb_bound;
int pcm_mode;
Ipp32s *filter_buf;
Ipp16s *pcm_filter;
Ipp32s *xr_buf;
Ipp32s *xrix_buf;
Ipp32s *overlap_buf;
Ipp8s *scale_factor;
Ipp8s *cur_scale_factor;
int *scfsi;
Ipp32s *current_ix_ptr;
int privatebits = 0;
int count1_len[4];
int huf_size[4];
int off_set;
int maindata_bits;
int maindata_begin;
int meanbits_num;
sample_status ret_code;
if(!(sound && stream_buf && enc_state)) {
return SAMPLE_STATUS_BADARG_ERR;
}
channel_num = sound->snd_channel_num ;
totalbuf_size = (MP3_PQMFDELAY_LEN + MP3_FRAME_LEN) * channel_num;
/* Shift the new input signal into the state buffer */
pcm_buf = enc_state->pcm_state_buf;
for(i = 0; i < totalbuf_size - channel_num * MP3_FRAME_LEN; i ++) {
pcm_buf[i] = pcm_buf[i + channel_num * MP3_FRAME_LEN];
}
for(i = 0; i < channel_num * MP3_FRAME_LEN; i ++) {
pcm_buf[totalbuf_size - channel_num * MP3_FRAME_LEN + i] =
sound->snd_frame[i];
}
/* pSbuf lenght is 576, work_buf in Quantize is 576+192= 768,
// length of XrIx is at least 2304 */
xrix_buf = enc_state->work_buf + 1000;
psy_state = enc_state->psy_state;
psy_info = enc_state->psy_info;
is_sfb_bound = enc_state->is_sfb_bound;
side_info = (IppMP3SideInfo *)enc_state->side_info;
frame_header = &(enc_state->frame_header);
work_buf = enc_state->work_buf;
pcm_mode = (channel_num == 2)?2:1;
meanbits_num = enc_state->meanbits_num;
maindata_bits = 0;
off_set = 0;
if(frame_header->id == MP3_MPEG1_ID){
granule_num = 2;
}else {
granule_num = 1;
}
/* Calculate the padding bits */
enc_state->rest -= enc_state->frame_byte_dif;
enc_state->frame_len[enc_state->bufferedframe_index] = \
enc_state->bytes_per_frame;
if(enc_state->rest < 0) {
enc_state->rest += mpeg1_samplerate_table[enc_state->sample_rate];
frame_header->paddingBit = 1;
meanbits_num += BITSPERBYTE_NUM;
enc_state->frame_len[enc_state->bufferedframe_index] ++;
} else {
frame_header->paddingBit = 0;
}
enc_state->mdframe_buf_ptr[enc_state->bufferedframe_index] = \
enc_state->cur_maindata_buf;
ret_code = ippsPsychoacousticModelTwo_MP3_16s(pcm_buf + \
MP3_PQMFPSYDELAY_LEN * channel_num, psy_info, is_sfb_bound,\
side_info, frame_header, psy_state, pcm_mode, work_buf);
if ( ret_code != ippStsNoErr ) {
return SAMPLE_STATUS_ERR;
}
frame_header->modeExt &= 0x2; /* disable IS now */
/* Begin the encoding processing, including filter band and stereo encoding */
filter_buf = enc_state->work_buf;
scale_factor = enc_state->scale_factor;
for(gr = 0; gr < granule_num; gr ++) {
xr_buf = xrix_buf + (gr * channel_num) * IPP_MP3_GRANULE_LEN;
for(ch = 0; ch < channel_num; ch ++) {
/* Apply hybrid the filter bank */
pcm_filter = pcm_buf + gr * channel_num * IPP_MP3_GRANULE_LEN + ch;
for(subband_num = 0; subband_num < 18; subband_num ++) {
ret_code = ippsAnalysisPQMF_MP3_16s32s(pcm_filter + \
MP3_SUBBAND_NUM * subband_num * channel_num,\
filter_buf + MP3_SUBBAND_NUM * subband_num, pcm_mode);
if ( ret_code != ippStsNoErr ) {
return SAMPLE_STATUS_ERR;
}
}
overlap_buf = enc_state->overlap_buf + ch * IPP_MP3_GRANULE_LEN;
ret_code = ippsMDCTFwd_MP3_32s(filter_buf, xr_buf + \
ch * IPP_MP3_GRANULE_LEN,
side_info[gr * channel_num + ch].blockType,
side_info[gr * channel_num + ch].mixedBlock, frame_header,
overlap_buf);
if ( ret_code != ippStsNoErr ) {
return SAMPLE_STATUS_ERR;
}
buffer_copy_audio(filter_buf, overlap_buf, IPP_MP3_GRANULE_LEN);
}
/* Stereo encoding */
if(channel_num == 2) {
ret_code = ippsJointStereoEncode_MP3_32s_I(
xr_buf , xr_buf + IPP_MP3_GRANULE_LEN, scale_factor \
+ (gr * channel_num + 1) * IPP_MP3_SF_BUF_LEN, frame_header,\
side_info + gr * channel_num, is_sfb_bound + gr * 3);
if ( ret_code != ippStsNoErr ) {
return SAMPLE_STATUS_ERR;
}
}
}
/* Quantization */
scfsi = enc_state->scfsi;
resv = &(enc_state->bit_resv);
maindata_begin = resv->BitsRemaining >> 3;
xr_buf = xrix_buf;
ret_code = ippsQuantize_MP3_32s_I(xr_buf, scale_factor, scfsi, count1_len,\
huf_size, frame_header, side_info, psy_info, psy_state,\
resv, meanbits_num, is_sfb_bound, work_buf);
if ( ret_code != ippStsNoErr ) {
return SAMPLE_STATUS_ERR;
}
/* Pack the encoded bitstream and noiseless encoding */
for(gr = 0; gr < granule_num; gr ++) {
for(ch = 0; ch < channel_num; ch ++) {
/* Pack scale factor data */
cur_scale_factor = scale_factor + \
(gr * channel_num + ch) * IPP_MP3_SF_BUF_LEN;
ippsPackScaleFactors_MP3_8s1u(cur_scale_factor, \
&(enc_state->cur_maindata_buf), &off_set, frame_header, \
side_info + gr * channel_num + ch, &(scfsi[ch * 4]), gr, ch);
current_ix_ptr = xr_buf + IPP_MP3_GRANULE_LEN * (gr * channel_num + ch);
current_side_info = side_info + gr * channel_num + ch;
/* Huffman encoding and pack the spectral data */
ret_code = ippsHuffmanEncode_MP3_32s1u(current_ix_ptr,
&(enc_state->cur_maindata_buf), &off_set, \
frame_header, current_side_info,
count1_len[ gr * channel_num + ch], huf_size[gr * channel_num + ch]);
if ( ret_code != ippStsNoErr ) {
return SAMPLE_STATUS_ERR;
}
maindata_bits += current_side_info->part23Len;
}
}
/* Pack the frame header and side information */
{
ret_code = ippsPackFrameHeader_MP3(frame_header, &(enc_state->hdsi_buf_ptr));
if ( ret_code != ippStsNoErr ) {
return SAMPLE_STATUS_ERR;
}
ret_code=ippsPackSideInfo_MP3(side_info, &(enc_state->hdsi_buf_ptr),\
maindata_begin, privatebits, scfsi, frame_header);
if ( ret_code != ippStsNoErr ) {
return SAMPLE_STATUS_ERR;
}
}
if ( enc_state->cur_maindata_buf - enc_state->maindata_buf > \
(MP3_MAINDATABUF_SIZE - 960 * channel_num) ) {
enc_state->cur_maindata_buf = enc_state->maindata_buf;
}
enc_state->mdframe_buf_len[enc_state->bufferedframe_index] =\
( maindata_bits + 7 ) >> 3;
enc_state->bufferedframe_num ++;
enc_state->bufferedframe_index ++;
if ( enc_state->bufferedframe_index == 9 ) {
enc_state->bufferedframe_index = 0;
enc_state->hdsi_buf_ptr = enc_state->hdsi_buf;
}
encoder_flushbitstream_mp3(enc_state, stream_buf);
return SAMPLE_STATUS_NOERR;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -