目录
实验原理
MPEG-1 Audio LayerII编码器原理
基本思想
两条线
时-频分析的矛盾
心理声学模型
MPEG-1音频编码器框架图
MPEG-1声音的主要性能
多相滤波器组
心理声学模型
比特分配器
装帧
实验要求
程序分析
调试及实验结果
输出音频的采样率和目标码率
输出某个数据帧所分配的比特数,比例因子,比特分配结果
结果分析
但是FFT变换是无法同时保证时域和频域的精细度,增加时域的分辨力,频域的分辨力就会下降,反之亦然。
听觉阈值
频域掩蔽
临界频带
多相滤波器缺点 :
1、将样本变换到频域(Layer I:每帧384个样本点,512个样本点足够覆盖;Layer II 和Layer III:每帧1152个样本点,每帧两次计算,模型1选择两个信号掩蔽比(SMR)中较小的一个)
main函数
int main (int argc, char **argv)
{
...
/***初始化及分配内存***/
static unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];//存放每个声道32个子带的比特分配数
static unsigned int scalar[2][3][SBLIMIT], j_scale[3][SBLIMIT]; //存放2声道3个组每组12个样值的32个子带的比例因子
...
int sb, ch, adb; //adb用于存放数据帧分配的比特数
...
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;
//get_audio()读取3*32*12=1152个音频样值,while循环获取每一帧信息
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++ )
for ( bl = 0; bl < 12; bl++ )
for ( ch = 0; ch < nch; ch++ )
WindowFilterSubband( &buffer[ch][gr * 12 * 32 + 32 * bl], ch,
&(*sb_sample)[ch][gr][bl][0] ); //多项滤波器组
}
#ifdef REFERENCECODE
{
...
}
#endif
#ifdef NEWENCODE
...
#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
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 根据心理声学模型计算SMR*/
switch (model) {
case -1:
psycho_n1 (smr, nch); //计算SMR
break;
case 0: /* Psy Model A */
psycho_0 (smr, nch, scalar, (FLOAT) s_freq[header.version][header.sampling_frequency] * 1000);
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
...
#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);
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
...
exit (0);
}
print_config()
void print_config(frame_info* frame, int* psy, char* inPath,
char* outPath) //可输出音频的采样率和目标码率
{
frame_header* header = frame->header;
if (glopts.verbosity == 0)
return;
fprintf(stderr, "--------------------------------------------\n");
fprintf(stderr, "Input File : '%s' %.1f kHz\n",
(strcmp(inPath, "-") ? inPath : "stdin"),
s_freq[header->version][header->sampling_frequency]);//输入文件路径和音频采样率
fprintf(stderr, "Output File: '%s'\n",
(strcmp(outPath, "-") ? outPath : "stdout"));//输出文件路径
fprintf(stderr, "%d kbps ", bitrate[header->version][header->bitrate_index]);//目标码率
fprintf(stderr, "%s ", version_names[header->version]);
...
}
在main函数中添加代码
FILE* output = NULL;
output = fopen("output.txt", "wb");
......
if (frameNum == 100) {
fprintf(output, "frameNum=%d\n", frameNum);
fprintf(output, "available_bits=%d\n", adb);///输出该帧的可用比特数
fprintf(output, "\nscale_factors:\n");//输出该帧的比例因子
for (int k = 0; k < nch; k++) {
fprintf(output, "channel[%d]\n", k);
for (int i = 0; i < frame.sblimit; i++) {
fprintf(output, "subband[%d]:", i);
for (int 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 == 100) {
int k, i;
fprintf(output, "\nbit_allocation:\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]);
}
}
}
得到output.txt