一、程序设计的整体框架
主函数及注释:
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]; //各个子带
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];
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++ ) //每12个样点一组
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
//选择合适的心理声学模型
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);
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);
}
二、感知音频编码的设计思想
基本思想:分析信号,去掉不能被感知的部分。
听觉阈值:
听觉掩蔽特性:
MPEG-1 Audio LayerII编码器:
该编码器的两条线: 1.码流经过滤波器组变为32个子带的频域信号,进行子带编码。
2.对码流做1024点fft变换,根据心理声学模型来分配比特数,进行编码。
时-频分析的矛盾: 时域取值间隔越短,频域带宽越宽,更难分析。
三、心理声学模型的实现过程
临界频带:
掩蔽值计算的思路:
四、码率分配的实现思路
对每个子带计算噪掩比NMR=SMR-SNR(dB)
对最高NMR的子带进行比特分配,使获益
最大的子带的量化级别增加一级,然后重新计算该子带的NMR,此时分配了更多比特的子带的信噪比(SNR)会提升,所以其NMR会下降。不断循环,直到没有比特可分配或者所有NMR都减到0。
五、输出音频的采样率和目标码率
查阅得原代码中带有输出采样频率和目标码率到屏幕的功能:
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]);//输出目标码率
六、输出某一数据帧的信息
为输出该帧所分配的比特数,该帧的比例因子,该帧的比特分配结果,在主函数的while循环中添加如下代码:
FILE* frame_info_file;
frame_info_file = fopen("C:\\Users\\tonym\\Desktop\\study\\shujuyasuo\\mpeg\\实验6_MPG音频编码\\m2aenc\\frame_info_file.txt", "w");
if (frameNum == 50)//此处设定想第几帧
{
fprintf(frame_info_file, "采样率:%f khz\n", s_freq[frame.header->version][frame.header->sampling_frequency]);
fprintf(frame_info_file, "目标码率:%d kbps\n", bitrate[frame.header->version][frame.header->bitrate_index]);
fprintf(frame_info_file, "第%d帧\n", frameNum);
fprintf(frame_info_file, "所分配比特数:%d\n", adb);
fprintf(frame_info_file, "比例因子:\n");
for (ch = 0; ch < nch; ch++)
{
fprintf(frame_info_file, "声道%d:\n", ch);
for (sb = 0; sb < frame.sblimit; sb++)
{
fprintf(frame_info_file, " 子带%2d:", sb);
for (int gr = 0; gr < 3; gr++)
{
fprintf(frame_info_file, "%4d", scalar[ch][gr][sb]);
}
fprintf(frame_info_file, "\n");
}
}
fprintf(frame_info_file, "\n");
fprintf(frame_info_file, "比特分配表:\n");
for (ch = 0; ch < nch; ch++)
{
fprintf(frame_info_file, "声道%d:\n", ch);
for (sb = 0; sb < frame.sblimit; sb++)
{
fprintf(frame_info_file, " 子带%2d:\t%d\n", sb, bit_alloc[ch][sb]);
}
fprintf(frame_info_file, "\n");
}
}
输出样例:
输入文件为音乐(.wav)时的输出文件:
采样率:44.100000 khz
目标码率:192 kbps
第50帧
所分配比特数:5008
比例因子:
声道0:
子带 0: 10 11 11
子带 1: 18 19 23
子带 2: 17 17 21
子带 3: 22 25 27
子带 4: 30 31 33
子带 5: 24 25 29
子带 6: 22 27 30
子带 7: 19 23 26
子带 8: 42 43 45
子带 9: 29 31 35
子带10: 29 30 31
子带11: 29 29 29
子带12: 21 24 28
子带13: 24 23 27
子带14: 24 26 28
子带15: 23 23 31
子带16: 30 32 33
子带17: 28 29 32
子带18: 26 26 30
子带19: 29 31 35
子带20: 31 32 35
子带21: 29 31 36
子带22: 40 42 44
子带23: 52 54 49
子带24: 52 55 53
子带25: 52 53 55
子带26: 53 52 51
子带27: 56 52 55
子带28: 53 55 53
子带29: 54 55 51
比特分配表:
声道0:
子带 0: 9
子带 1: 8
子带 2: 8
子带 3: 8
子带 4: 6
子带 5: 7
子带 6: 7
子带 7: 7
子带 8: 3
子带 9: 6
子带10: 6
子带11: 6
子带12: 7
子带13: 6
子带14: 6
子带15: 6
子带16: 5
子带17: 5
子带18: 6
子带19: 4
子带20: 4
子带21: 4
子带22: 0
子带23: 0
子带24: 0
子带25: 0
子带26: 0
子带27: 0
子带28: 0
子带29: 0
输入文件为噪声(.wav)时的输出文件:
采样率:48.000000 khz
目标码率:192 kbps
第50帧
所分配比特数:4608
比例因子:
声道0:
子带 0: 24 23 24
子带 1: 26 26 27
子带 2: 28 25 27
子带 3: 28 28 26
子带 4: 25 24 26
子带 5: 25 26 25
子带 6: 29 27 28
子带 7: 32 31 31
子带 8: 31 31 33
子带 9: 32 32 33
子带10: 33 32 35
子带11: 37 35 38
子带12: 35 34 36
子带13: 35 34 34
子带14: 35 36 37
子带15: 39 36 37
子带16: 37 37 40
子带17: 40 40 40
子带18: 43 43 43
子带19: 45 44 43
子带20: 46 45 45
子带21: 50 48 47
子带22: 57 59 59
子带23: 60 57 60
子带24: 56 57 58
子带25: 58 57 57
子带26: 57 57 58
比特分配表:
声道0:
子带 0: 8
子带 1: 7
子带 2: 7
子带 3: 9
子带 4: 9
子带 5: 8
子带 6: 8
子带 7: 8
子带 8: 8
子带 9: 7
子带10: 7
子带11: 6
子带12: 6
子带13: 6
子带14: 6
子带15: 6
子带16: 6
子带17: 5
子带18: 5
子带19: 0
子带20: 0
子带21: 0
子带22: 0
子带23: 0
子带24: 0
子带25: 0
子带26: 0
输入为音乐与噪声混合(.wav)时的输出文件:
采样率:44.100000 khz
目标码率:192 kbps
第50帧
所分配比特数:5008
比例因子:
声道0:
子带 0: 12 11 11
子带 1: 15 16 16
子带 2: 17 15 17
子带 3: 20 22 21
子带 4: 19 20 18
子带 5: 18 18 19
子带 6: 19 20 20
子带 7: 20 21 21
子带 8: 25 26 27
子带 9: 25 26 25
子带10: 28 26 28
子带11: 26 28 29
子带12: 27 25 24
子带13: 24 26 21
子带14: 25 25 23
子带15: 26 24 24
子带16: 29 28 28
子带17: 30 29 27
子带18: 29 28 24
子带19: 29 32 32
子带20: 30 32 31
子带21: 29 30 32
子带22: 38 38 41
子带23: 58 55 57
子带24: 57 56 58
子带25: 59 60 58
子带26: 58 58 59
子带27: 57 58 57
子带28: 57 58 56
子带29: 58 54 59
声道1:
子带 0: 12 11 11
子带 1: 15 16 16
子带 2: 17 16 16
子带 3: 19 22 21
子带 4: 20 19 19
子带 5: 17 18 19
子带 6: 18 21 19
子带 7: 20 21 23
子带 8: 24 27 27
子带 9: 26 27 26
子带10: 27 27 28
子带11: 26 28 31
子带12: 27 27 24
子带13: 25 27 22
子带14: 24 24 22
子带15: 26 24 23
子带16: 29 28 29
子带17: 29 30 29
子带18: 27 28 23
子带19: 30 31 32
子带20: 30 31 32
子带21: 29 30 32
子带22: 37 39 43
子带23: 57 58 58
子带24: 59 57 57
子带25: 58 58 59
子带26: 59 58 58
子带27: 56 58 58
子带28: 57 58 56
子带29: 59 54 58
比特分配表:
声道0:
子带 0: 5
子带 1: 4
子带 2: 3
子带 3: 4
子带 4: 4
子带 5: 4
子带 6: 4
子带 7: 3
子带 8: 1
子带 9: 3
子带10: 2
子带11: 3
子带12: 3
子带13: 3
子带14: 2
子带15: 1
子带16: 1
子带17: 2
子带18: 3
子带19: 1
子带20: 1
子带21: 0
子带22: 0
子带23: 0
子带24: 0
子带25: 0
子带26: 0
子带27: 0
子带28: 0
子带29: 0
声道1:
子带 0: 5
子带 1: 4
子带 2: 3
子带 3: 4
子带 4: 5
子带 5: 3
子带 6: 4
子带 7: 3
子带 8: 1
子带 9: 3
子带10: 3
子带11: 2
子带12: 3
子带13: 2
子带14: 2
子带15: 1
子带16: 1
子带17: 1
子带18: 3
子带19: 1
子带20: 1
子带21: 0
子带22: 0
子带23: 0
子带24: 0
子带25: 0
子带26: 0
子带27: 0
子带28: 0
子带29: 0