本次实验基于mpeg1audio-layer2进行分析学习
mpeg-1 audio layer1/2/3,都基于mepg1标准。layer1/2/3的区别是编码的复杂度由低到高增加。
2 pcm 码流
PCM 脉冲编码调制即Pulse Code Modulation。脉冲编码调制是数字通信的编码方式之一。将模拟信号每隔一定时间进行取样,使其离散化,同时将抽样值量化,用一组二进制码来表示抽样脉冲的幅值。
3 子带编码
子带编码(Subband Coding)简称SBC,根据信号频谱,将信号分解成不同频带分量便于根据人耳听觉特性等信息进行不同的处理
4 人耳听觉特性
1)人耳灵敏度与频率有关
2)人耳存在掩蔽效应
5 心理声学模型
根据人耳听觉特性所设计,决定各个子带中允许的最大量化噪声
对main函数进行了注释
int main (int argc, char **argv)
{
typedef double SBS[2][3][SCALE_BLOCK][SBLIMIT];
SBS *sb_sample;
typedef double JSBS[3][SCALE_BLOCK][SBLIMIT];
JSBS *j_sample;
typedef double IN[2][HAN_SIZE];
IN *win_que;
typedef unsigned int SUB[2][3][SCALE_BLOCK][SBLIMIT];
SUB *subband;
frame_info frame;//包含头信息等内容
frame_header header;//头信息具体内容
char original_file_name[MAX_NAME_SIZE];//原始文件名
char encoded_file_name[MAX_NAME_SIZE];//编码文件名
short **win_buf;
static short buffer[2][1152];
static unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];
static unsigned int scalar[2][3][SBLIMIT], j_scale[3][SBLIMIT];
static double smr[2][SBLIMIT], lgmin[2][SBLIMIT], max_sc[2][SBLIMIT];
// FLOAT snr32[32];
short sam[2][1344]; /* was [1056]; */
int model, nch, error_protection;
static unsigned int crc;
int sb, ch, adb;
unsigned long frameBits, sentBits = 0;
unsigned long num_samples;
int lg_frame;
int i;
/* Used to keep the SNR values for the fast/quick psy models */
static FLOAT smrdef[2][32];//共32个子带
static int psycount = 0;
extern int minimum;
time_t start_time, end_time;
int total_time;
sb_sample = (SBS *) mem_alloc (sizeof (SBS), "sb_sample");
j_sample = (JSBS *) mem_alloc (sizeof (JSBS), "j_sample");
win_que = (IN *) mem_alloc (sizeof (IN), "Win_que");
subband = (SUB *) mem_alloc (sizeof (SUB), "subband");
win_buf = (short **) mem_alloc (sizeof (short *) * 2, "win_buf");
/* clear buffers */
memset ((char *) buffer, 0, sizeof (buffer));
memset ((char *) bit_alloc, 0, sizeof (bit_alloc));
memset ((char *) scalar, 0, sizeof (scalar));
memset ((char *) j_scale, 0, sizeof (j_scale));
memset ((char *) scfsi, 0, sizeof (scfsi));
memset ((char *) smr, 0, sizeof (smr));
memset ((char *) lgmin, 0, sizeof (lgmin));
memset ((char *) max_sc, 0, sizeof (max_sc));
//memset ((char *) snr32, 0, sizeof (snr32));
memset ((char *) sam, 0, sizeof (sam));
//初始化相关buffer
global_init ();
//运行一次初始化函数
header.extension = 0;
frame.header = &header;
frame.tab_num = -1; /* no table loaded */
frame.alloc = NULL;
header.version = MPEG_AUDIO_ID; /* Default: MPEG-1 */
total_time = 0;
time(&start_time);//记录编码开始时间
programName = argv[0];//读入命令行参数
if (argc == 1) /* no command-line args */
short_usage ();
else
parse_args (argc, argv, &frame, &model, &num_samples, original_file_name,
encoded_file_name);
print_config (&frame, &model, original_file_name, encoded_file_name);
/* this will load the alloc tables and do some other stuff */
hdr_to_frps (&frame);//读文件头内相关信息
nch = frame.nch;
error_protection = header.error_protection;
//获取音频信息
while (get_audio (musicin, buffer, num_samples, nch, &header) > 0) {
if (glopts.verbosity > 1)
if (++frameNum % 10 == 0)
fprintf (stderr, "[%4u]\r", frameNum);
fflush (stderr);
win_buf[0] = &buffer[0][0];
win_buf[1] = &buffer[1][0];
//比特预算
adb = available_bits (&header, &glopts);
lg_frame = adb / 8;
if (header.dab_extension) {
/* in 24 kHz we always have 4 bytes */
if (header.sampling_frequency == 1)
header.dab_extension = 4;
/* You must have one frame in memory if you are in DAB mode */
/* in conformity of the norme ETS 300 401 http://www.etsi.org */
/* see bitstream.c */
if (frameNum == 1)
minimum = lg_frame + MINIMUM;
adb -= header.dab_extension * 8 + header.dab_length * 8 + 16;
}
{
int gr, bl, ch;
/* New polyphase filter
Combines windowing and filtering. Ricardo Feb'03 */
for( gr = 0; gr < 3; gr++ )//layer2进行三次
for ( bl = 0; bl < 12; bl++ )//每个子带12个样本
for ( ch = 0; ch < nch; ch++ )//32个子带
WindowFilterSubband( &buffer[ch][gr * 12 * 32 + 32 * bl], ch,
&(*sb_sample)[ch][gr][bl][0] );
}//多项滤波器组
#ifdef REFERENCECODE
{
/* Old code. left here for reference */
int gr, bl, ch;
for (gr = 0; gr < 3; gr++)
for (bl = 0; bl < SCALE_BLOCK; bl++)
for (ch = 0; ch < nch; ch++) {
window_subband (&win_buf[ch], &(*win_que)[ch][0], ch);
filter_subband (&(*win_que)[ch][0], &(*sb_sample)[ch][gr][bl][0]);
}
}
#endif
#ifdef NEWENCODE
scalefactor_calc_new(*sb_sample, scalar, nch, frame.sblimit);
find_sf_max (scalar, &frame, max_sc);
if (frame.actual_mode == MPG_MD_JOINT_STEREO) {
/* this way we calculate more mono than we need */
/* but it is cheap */
combine_LR_new (*sb_sample, *j_sample, frame.sblimit);
scalefactor_calc_new (j_sample, &j_scale, 1, frame.sblimit);
}
#else
scale_factor_calc (*sb_sample, scalar, nch, frame.sblimit);
pick_scale (scalar, &frame, max_sc);//找到最大的比例因子
if (frame.actual_mode == MPG_MD_JOINT_STEREO) {
/* this way we calculate more mono than we need */
/* but it is cheap */
combine_LR (*sb_sample, *j_sample, frame.sblimit);
scale_factor_calc (j_sample, &j_scale, 1, frame.sblimit);
}
#endif
//选择合适的心理声学模型计算smr
if ((glopts.quickmode == TRUE) && (++psycount % glopts.quickcount != 0)) {
/* We're using quick mode, so we're only calculating the model every
'quickcount' frames. Otherwise, just copy the old ones across */
for (ch = 0; ch < nch; ch++) {
for (sb = 0; sb < SBLIMIT; sb++)
smr[ch][sb] = smrdef[ch][sb];
}
} else {
/* calculate the psymodel */
switch (model) {
case -1:
psycho_n1 (smr, nch);
break;
case 0: /* Psy Model A */
psycho_0 (smr, nch, scalar, (FLOAT) s_freq[header.version][header.sampling_frequency] * 1000);
break;
case 1:
psycho_1 (buffer, max_sc, smr, &frame);
break;
case 2:
for (ch = 0; ch < nch; ch++) {
psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
(FLOAT) s_freq[header.version][header.sampling_frequency] *
1000, &glopts);
}
break;
case 3:
/* Modified psy model 1 */
psycho_3 (buffer, max_sc, smr, &frame, &glopts);
break;
case 4:
/* Modified Psycho Model 2 */
for (ch = 0; ch < nch; ch++) {
psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
(FLOAT) s_freq[header.version][header.sampling_frequency] *
1000, &glopts);
}
break;
case 5:
/* Model 5 comparse model 1 and 3 */
psycho_1 (buffer, max_sc, smr, &frame);
fprintf(stdout,"1 ");
smr_dump(smr,nch);
psycho_3 (buffer, max_sc, smr, &frame, &glopts);
fprintf(stdout,"3 ");
smr_dump(smr,nch);
break;
case 6:
/* Model 6 compares model 2 and 4 */
for (ch = 0; ch < nch; ch++)
psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
(FLOAT) s_freq[header.version][header.sampling_frequency] *
1000, &glopts);
fprintf(stdout,"2 ");
smr_dump(smr,nch);
for (ch = 0; ch < nch; ch++)
psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
(FLOAT) s_freq[header.version][header.sampling_frequency] *
1000, &glopts);
fprintf(stdout,"4 ");
smr_dump(smr,nch);
break;
case 7:
fprintf(stdout,"Frame: %i\n",frameNum);
/* Dump the SMRs for all models */
psycho_1 (buffer, max_sc, smr, &frame);
fprintf(stdout,"1");
smr_dump(smr, nch);
psycho_3 (buffer, max_sc, smr, &frame, &glopts);
fprintf(stdout,"3");
smr_dump(smr,nch);
for (ch = 0; ch < nch; ch++)
psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
(FLOAT) s_freq[header.version][header.sampling_frequency] *
1000, &glopts);
fprintf(stdout,"2");
smr_dump(smr,nch);
for (ch = 0; ch < nch; ch++)
psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
(FLOAT) s_freq[header.version][header.sampling_frequency] *
1000, &glopts);
fprintf(stdout,"4");
smr_dump(smr,nch);
break;
case 8:
/* Compare 0 and 4 */
psycho_n1 (smr, nch);
fprintf(stdout,"0");
smr_dump(smr,nch);
for (ch = 0; ch < nch; ch++)
psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
(FLOAT) s_freq[header.version][header.sampling_frequency] *
1000, &glopts);
fprintf(stdout,"4");
smr_dump(smr,nch);
break;
default:
fprintf (stderr, "Invalid psy model specification: %i\n", model);
exit (0);
}
if (glopts.quickmode == TRUE)
/* copy the smr values and reuse them later */
for (ch = 0; ch < nch; ch++) {
for (sb = 0; sb < SBLIMIT; sb++)
smrdef[ch][sb] = smr[ch][sb];
}
if (glopts.verbosity > 4)
smr_dump(smr, nch);
}
#ifdef NEWENCODE
sf_transmission_pattern (scalar, scfsi, &frame);
main_bit_allocation_new (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
//main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
if (error_protection)
CRC_calc (&frame, bit_alloc, scfsi, &crc);
write_header (&frame, &bs);
//encode_info (&frame, &bs);
if (error_protection)
putbits (&bs, crc, 16);
write_bit_alloc (bit_alloc, &frame, &bs);
//encode_bit_alloc (bit_alloc, &frame, &bs);
write_scalefactors(bit_alloc, scfsi, scalar, &frame, &bs);
//encode_scale (bit_alloc, scfsi, scalar, &frame, &bs);
subband_quantization_new (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
*subband, &frame);
//subband_quantization (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
// *subband, &frame);
write_samples_new(*subband, bit_alloc, &frame, &bs);
//sample_encoding (*subband, bit_alloc, &frame, &bs);
#else
transmission_pattern (scalar, scfsi, &frame);
main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);//比特分配
if (error_protection)
CRC_calc (&frame, bit_alloc, scfsi, &crc);//crc纠错
encode_info (&frame, &bs);
if (error_protection)
encode_CRC (crc, &bs);
encode_bit_alloc (bit_alloc, &frame, &bs);
encode_scale (bit_alloc, scfsi, scalar, &frame, &bs);//编码
subband_quantization (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
*subband, &frame);//对子带进行量化
sample_encoding (*subband, bit_alloc, &frame, &bs);
#endif
/* If not all the bits were used, write out a stack of zeros */
for (i = 0; i < adb; i++)
put1bit (&bs, 0);
if (header.dab_extension) {
/* Reserve some bytes for X-PAD in DAB mode */
putbits (&bs, 0, header.dab_length * 8);//码流输出
for (i = header.dab_extension - 1; i >= 0; i--) {
CRC_calcDAB (&frame, bit_alloc, scfsi, scalar, &crc, i);
/* this crc is for the previous frame in DAB mode */
if (bs.buf_byte_idx + lg_frame < bs.buf_size)
bs.buf[bs.buf_byte_idx + lg_frame] = crc;
/* reserved 2 bytes for F-PAD in DAB mode */
putbits (&bs, crc, 8);
}
putbits (&bs, 0, 16);
}
frameBits = sstell (&bs) - sentBits;
if (frameBits % 8) {
/* a program failure */
fprintf (stderr, "Sent %ld bits = %ld slots plus %ld\n", frameBits,
frameBits / 8, frameBits % 8);
fprintf (stderr, "If you are reading this, the program is broken\n");
fprintf (stderr, "email [mfc at NOTplanckenerg.com] without the NOT\n");
fprintf (stderr, "with the command line arguments and other info\n");
exit (0);
}
理解感知音频编码的设计思想
1)两条线
MPEG-1的音频编码器有两条主线
一是码流读入之后经过滤波器组将PCM样本变换为32个子带的频域信号,而后再进行相关计算的子带编码。二是利用心理声学模型,对人耳不敏感的信号分配较少的比特,对人耳感知不到的信号不进行编码,去除冗余信息。
2)时-频分析的矛盾
根据傅里叶变换的性质,时域取值间隔越短,频域频带越宽,分析难度增加
理解心理声学模型的实现过程
1)临界频带的概念
定义一:当某个纯音被以它为中心频率、且具有一定带宽的连续噪声所掩蔽时,如果该纯音刚好被听到时的功率等于这一频带内的噪声功率,这个带宽为临界频带宽度;
定义二:如果增大掩蔽信号覆盖的频率范围,超过到某个值后掩蔽效应不再随着带宽的增大而增强,这个临界的带宽就称为临界频带。
2)掩蔽值计算的思路
单个掩蔽阈值的计算:音调成分和非音调成分单个掩蔽阈值根据标准中给出的算法求得。
全局掩蔽阈值的计算:
理解码率分配的实现思路
使整帧和每个子带的总噪声—掩蔽比最小,循环,直到没有比特可用
计算噪声-掩蔽比:
NMR = SMR – SNR (dB)
对最高NMR的子带分配比特,使获益最大的子带的量化级别增加一级,重新计算分配了更多比特子带的NMR
(SNR 由MPEG-I标准给定 (为量化水平的函数) NMR:表示波形误差与感知测量之间的误差)
输出音频的采样率和目标码率 ;
选择三个不同特性的音频文件(噪音、音乐、混合),对某个数据帧,输出:1)该帧所分配的比特数 2)该帧的比例因子 3)该帧的比特分配结果
(调试时出现若干error C2065:未声明的标识符错误 ,调整了部分变量定义位置后错误消失)
添加的代码
if(frameNum==20)
{
int k,t,i;
fprintf(output,"采样率=%.1f khz\n",s_freq[header.version][header.sampling_frequency]);
fprintf(output,"目标码率=%d kbps\n", bitrate[header.version][header.bitrate_index]);
fprintf(output,"第%d帧\n",frameNum);
fprintf(output,"可用比特数=%d\n",adb);
fprintf(output,"比例因子=\n");
for( k=0; k<nch ; k++ )
{
fprintf(output,"channel[%d]\n",k);
for(i=0;i<frame.sblimit;i++)
{
fprintf(output,"subband[%d]:",i);
原有代码 for( t=0;t<3;t++)
{
fprintf(output,"%d\t",scalar[k][t][i]);
}
fprintf(output,"\n");
}
}
}
transmission_pattern (scalar, scfsi, &frame);//原有代码
main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);//比特分配//原有代码
if(frameNum==20)
{
int k,i;
fprintf(output,"比特分配=\n");
for(k=0;k<nch;k++)
{
fprintf(output,"channel[%d]\n",k);
for(i=0;i<frame.sblimit;i++)
{
fprintf(output,"subband[%d]:%d\n",i,bit_alloc[k][i]);
}
}
}
采样率=22.1 khz
目标码率=96 kbps
第20帧
可用比特数=5016
比例因子=
channel[0]
subband[0]:9 12 10
subband[1]:10 13 10
subband[2]:10 10 10
subband[3]:9 10 10
subband[4]:10 11 12
subband[5]:12 10 12
subband[6]:13 12 13
subband[7]:13 12 11
subband[8]:11 9 9
subband[9]:12 10 11
subband[10]:12 9 11
subband[11]:11 12 13
subband[12]:11 13 13
subband[13]:10 10 12
subband[14]:11 13 12
subband[15]:8 10 13
subband[16]:9 11 11
subband[17]:11 12 12
subband[18]:13 12 11
subband[19]:11 12 13
subband[20]:11 10 12
subband[21]:9 10 9
subband[22]:13 12 11
subband[23]:10 13 12
subband[24]:12 12 11
subband[25]:13 11 13
subband[26]:10 11 9
subband[27]:12 10 12
subband[28]:10 11 11
subband[29]:13 12 11
比特分配=
channel[0]
subband[0]:8
subband[1]:8
subband[2]:8
subband[3]:7
subband[4]:6
subband[5]:6
subband[6]:5
subband[7]:5
subband[8]:6
subband[9]:5
subband[10]:5
subband[11]:3
subband[12]:3
subband[13]:3
subband[14]:3
subband[15]:3
subband[16]:3
subband[17]:3
subband[18]:3
subband[19]:3
subband[20]:3
subband[21]:3
subband[22]:3
subband[23]:3
subband[24]:3
subband[25]:3
subband[26]:3
subband[27]:3
subband[28]:3
subband[29]:3
采样率=44.1 khz
目标码率=192 kbps
第20帧
可用比特数=5016
比例因子=
channel[0]
subband[0]:14 13 13
subband[1]:13 13 16
subband[2]:13 14 14
subband[3]:12 15 14
subband[4]:14 14 15
subband[5]:14 13 15
subband[6]:13 17 13
subband[7]:14 14 14
subband[8]:13 12 15
subband[9]:13 14 13
subband[10]:13 13 13
subband[11]:14 14 15
subband[12]:11 13 16
subband[13]:12 13 13
subband[14]:14 13 14
subband[15]:13 17 13
subband[16]:13 15 15
subband[17]:13 13 14
subband[18]:12 13 14
subband[19]:16 13 13
subband[20]:15 12 14
subband[21]:13 13 15
subband[22]:15 12 13
subband[23]:12 13 15
subband[24]:13 12 13
subband[25]:13 13 14
subband[26]:13 12 14
subband[27]:11 14 14
subband[28]:13 13 12
subband[29]:15 13 14
channel[1]
subband[0]:13 13 14
subband[1]:14 13 13
subband[2]:12 14 15
subband[3]:12 15 14
subband[4]:13 14 14
subband[5]:13 13 13
subband[6]:14 14 14
subband[7]:13 14 14
subband[8]:14 12 15
subband[9]:17 14 14
subband[10]:14 13 13
subband[11]:15 15 15
subband[12]:13 13 14
subband[13]:14 13 15
subband[14]:15 15 16
subband[15]:12 13 12
subband[16]:12 13 14
subband[17]:14 15 14
subband[18]:12 13 12
subband[19]:14 15 13
subband[20]:13 13 14
subband[21]:12 15 13
subband[22]:16 14 12
subband[23]:10 15 13
subband[24]:13 15 14
subband[25]:13 14 14
subband[26]:13 15 12
subband[27]:13 14 13
subband[28]:14 14 13
subband[29]:15 15 13
比特分配=
channel[0]
subband[0]:4
subband[1]:3
subband[2]:3
subband[3]:4
subband[4]:4
subband[5]:4
subband[6]:4
subband[7]:3
subband[8]:3
subband[9]:3
subband[10]:2
subband[11]:3
subband[12]:3
subband[13]:3
subband[14]:2
subband[15]:2
subband[16]:2
subband[17]:3
subband[18]:3
subband[19]:1
subband[20]:1
subband[21]:2
subband[22]:2
subband[23]:2
subband[24]:1
subband[25]:1
subband[26]:1
subband[27]:2
subband[28]:1
subband[29]:0
channel[1]
subband[0]:4
subband[1]:3
subband[2]:3
subband[3]:4
subband[4]:4
subband[5]:3
subband[6]:3
subband[7]:3
subband[8]:3
subband[9]:3
subband[10]:2
subband[11]:3
subband[12]:3
subband[13]:3
subband[14]:2
subband[15]:2
subband[16]:2
subband[17]:3
subband[18]:3
subband[19]:1
subband[20]:1
subband[21]:2
subband[22]:2
subband[23]:2
subband[24]:1
subband[25]:1
subband[26]:1
subband[27]:2
subband[28]:1
subband[29]:0
采样率=22.1 khz
目标码率=96 kbps
第20帧
可用比特数=5016
比例因子=
channel[0]
subband[0]:7 8 9
subband[1]:10 11 12
subband[2]:10 11 10
subband[3]:12 13 11
subband[4]:12 12 14
subband[5]:12 11 12
subband[6]:12 13 14
subband[7]:12 12 13
subband[8]:13 10 12
subband[9]:13 13 15
subband[10]:12 12 12
subband[11]:13 10 12
subband[12]:12 13 13
subband[13]:13 12 13
subband[14]:13 12 12
subband[15]:12 13 11
subband[16]:10 12 12
subband[17]:11 14 13
subband[18]:12 12 13
subband[19]:13 13 11
subband[20]:13 14 14
subband[21]:10 13 11
subband[22]:10 11 12
subband[23]:13 12 12
subband[24]:13 12 13
subband[25]:12 13 12
subband[26]:12 11 13
subband[27]:14 13 13
subband[28]:14 12 15
subband[29]:11 11 13
比特分配=
channel[0]
subband[0]:8
subband[1]:7
subband[2]:8
subband[3]:7
subband[4]:6
subband[5]:6
subband[6]:6
subband[7]:5
subband[8]:6
subband[9]:5
subband[10]:5
subband[11]:3
subband[12]:3
subband[13]:3
subband[14]:3
subband[15]:3
subband[16]:3
subband[17]:3
subband[18]:3
subband[19]:3
subband[20]:3
subband[21]:3
subband[22]:3
subband[23]:3
subband[24]:3
subband[25]:3
subband[26]:3
subband[27]:3
subband[28]:3
subband[29]:3