1、多相滤波器组(Polyphase Filter Bank)
将PCM样本变换到32个子带的频域信号。高时域分辨率和高频域分辨率是不可兼得的,需要做出权衡。
滤波器组的输出是临界频带经过量化的系数样值。若一个子带覆盖多个临界频带,则选择具有最小NMR的临界频带来计算分配给子带的比特数。
2、MPEG-I 心理声学模型
心理声学模型决定了各个子带中允许的最大量化噪声,小于它的量化噪声都会被掩蔽。若子带内的信号功率小于掩蔽阈值,则不进行编码;否则,确定要编码的系数所需的比特数,使量化引起的噪声低于掩蔽效应。
(5)音调和非音调掩蔽成分的消除
利用标准中给出的绝对阈值消除被掩蔽成分;
考虑在每个临界频带内,小于0.5Bark的距离中只保留最高功率的成分
(6)单个掩蔽阈值的计算
音调成分和非音调成分单个掩蔽阈值根据标准中给出的算法求得。
(7)全局掩蔽阈值的计算
还要考虑别的临界频带的影响。一个掩蔽信号会对其它频带上的信号产生掩蔽效应。这种掩蔽效应称为掩蔽扩散。
(8)每个子带的掩蔽阈值
选择出本子带中最小的阈值作为子带阈值。
对高频不正确——高频区的临界频带很宽,可能跨越多个子带,从而导致模型1将临界带宽内所有的非音调部分集中为一个代表频率,当一个子带在很宽的频带内却远离代表频率时,无法得到准确的非音调掩蔽值。但计算量低。
(9)计算每个子带信号掩蔽比(signal-to-mask ratio, SMR)
SMR = 信号能量 / 掩蔽阈值,并将SMR传递给编码单元
步骤:
(1)比特分配
在调整到固定的码率之前,先确定可用于样值编码的有效比特数。这个数值取决于比例因子、比例因子选择信息、比特分配信息以及辅助数据所需比特数。
过程:
对每个子带计算掩噪比MNR,是信噪比SNR – 信掩比SMR,即:MNR = SNR–SMR。然后找出其中具有最低MNR的子带,并给该子带多分配一些比特,然后重新计算MNR,继续分配,重复该步骤,直至没有比特可以分配。这样可以使得在满足比特率和掩蔽要求的前提下,使MNR最小。其中SNR 由MPEG-I标准给定 (为量化水平的函数) ,NMR:表示波形误差与感知测量之间的误差。
(2) 计算比例因子
对各个子带每36个样点(Layer I为12个样点)进行一次比例因子的计算,先确定12个连续样值中的最大值,查Layer II、Layer I比例因子表中比这它大的最小值作为量化比例因子;
每12个样值计算出一个比例因子,Layer II中将每个子带分为3组,每组各有12个取样值,因此36个样值具有3个比例因子;
比例因子可以使得比较准确地计算出子带的声压级;
一般比例因子从低频子带到高频子带连续下降;
(3) 子带样值量化
将子带样值除以比例因子(结果为XXX),根据所分配的比特数查表得AAA、BBB,量化结果为AX+BAX+BAX+B。
(4) 颗粒形成
对量化级别在3、5、9级时,采用“颗粒” 优化。例如:
采用颗粒量化:3个样本 @ 3个量化水平 = 27种可能的值 → 5 bit
不采用颗粒量化:1个样本 @ 3个量化水平 = 2bit → 3个样本6 bit
经过大量实验,使用颗粒量化可将压缩比从4:1增加到6:1乃至8:1
其中每个子带中前后相邻的连续36个样值(3组12个样值)共用在这个子带比特分配值,36个样值中的同一组样值共用该组的比例因子;
同一时刻的32个子带样值放在一起;
Layer II每帧包含1152个PCM样值(为Layer I的三倍);
若取样频率为48 kHz,一帧相当于1152 / 48k = 24 ms的声音样值,因此Layer II的精确度为24 ms(为Layer I的三倍,因而更精确)。
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]; // 存放双声道各个子带的比特分配表
static unsigned int scfsi[2][SBLIMIT];
static unsigned int scalar[2][3][SBLIMIT]; // 存放双声道3组12个样值的各个子带的比例因子
static unsigned int 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;
int nch; // 声道数
int error_protection;
static unsigned int crc;
int sb, ch;
int adb; // 比特预算 (i.e., number of bits available)
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];
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));
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]; // exe文件名称
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); // print文件参数到窗口
/* 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) {
/* 从输入的文件读取数据到buffer */
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++) /* 36个样值分为3组 */
for (bl = 0; bl < 12; bl++) /* 每组做12次子带分解 */
for (ch = 0; ch < nch; ch++)
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); // smr为输出
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);
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);
}
sentBits += frameBits;
}
close_bit_stream_w(&bs);
if ((glopts.verbosity > 1) && (glopts.vbr == TRUE)) {
int i;
#ifdef NEWENCODE
extern int vbrstats_new[15];
#else
extern int vbrstats[15];
#endif
fprintf(stdout, "VBR stats:\n");
for (i = 1; i < 15; i++)
fprintf(stdout, "%4i ", bitrate[header.version][i]);
fprintf(stdout, "\n");
for (i = 1; i < 15; i++)
#ifdef NEWENCODE
fprintf(stdout, "%4i ", vbrstats_new[i]);
#else
fprintf(stdout, "%4i ", vbrstats[i]);
#endif
fprintf(stdout, "\n");
}
fprintf(stderr,
"Avg slots/frame = %.3f; b/smp = %.2f; bitrate = %.3f kbps\n",
(FLOAT)sentBits / (frameNum * 8),
(FLOAT)sentBits / (frameNum * 1152),
(FLOAT)sentBits / (frameNum * 1152) *
s_freq[header.version][header.sampling_frequency]);
if (fclose(musicin) != 0) {
fprintf(stderr, "Could not close \"%s\".\n", original_file_name);
exit(2);
}
fprintf(stderr, "\nDone\n");
time(&end_time);
total_time = end_time - start_time;
printf("total time is %d\n", total_time);
exit(0);
}
以test文件夹中的test.wav为例
添加输入语句后可得
音频采样率:44.1KHz
目标码率:192Kbps
可用比特数:5008
比例因子:
声道1:
子带0: 12 12 11
子带1: 12 13 12
子带2: 21 18 18
子带3: 27 27 25
子带4: 31 29 29
子带5: 28 23 26
子带6: 22 22 22
子带7: 22 21 26
子带8: 32 28 28
子带9: 34 31 30
子带10: 34 32 31
子带11: 30 30 26
子带12: 27 24 26
子带13: 24 23 24
子带14: 26 22 25
子带15: 30 25 27
子带16: 27 26 29
子带17: 32 30 29
子带18: 31 33 30
子带19: 28 26 26
子带20: 34 34 31
子带21: 34 31 32
子带22: 38 38 41
子带23: 39 52 50
子带24: 43 51 57
子带25: 41 55 54
子带26: 45 54 52
子带27: 42 55 54
子带28: 44 52 54
子带29: 43 52 53
子带30: 0 0 0
子带31: 0 0 0
比特分配
声道1:
子带0: 8
子带1: 8
子带2: 6
子带3: 8
子带4: 7
子带5: 8
子带6: 8
子带7: 6
子带8: 5
子带9: 6
子带10: 6
子带11: 7
子带12: 6
子带13: 6
子带14: 6
子带15: 5
子带16: 5
子带17: 5
子带18: 4
子带19: 6
子带20: 3
子带21: 3
子带22: 0
子带23: 0
子带24: 0
子带25: 0
子带26: 0
子带27: 0
子带28: 0
子带29: 0
子带30: 0
子带31: 0