1.实验原理
原理框图:
各模块作用:
- 心理声学模型:依据人耳听觉阈值和听觉掩蔽特性建立,分析听阈曲线,保证满足人耳听觉感受的前提下,减少码率。
- 动态比特分配:根据目标码率和心理声学模型分析的结果,为子带分配最合理的量化比特数,使整帧和每个子带的噪掩比NMR最小的算法。(该信息需送至解码端,以供解码端完成解码)
- 滤波器组:划分32个子带,每个子带对应一个频段,每个子带含有12个样点(MPEG-2为12*3=36个样点)。由于这些样点之间的间隔的时间较短,在取值上不会发生太大的变化,便于降低量化比特数。
- 线性量化器:根据动态比特分配给出的量化比特数,对子带的数据进行线性量化。
- 颗粒形成:将量化后的数据形成传输的数据流。
2.实验内容
1.程序
实验主程序
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];//存放两个声道各子带的比特分配表,SBLIMIT=32即32个子带
static unsigned int scalar[2][3][SBLIMIT], j_scale[3][SBLIMIT];//存放各子带每组12个样值的比例因子
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++ )
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] );
}
…………
输出音频的采样率和目标码率
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]);
if (header->mode != MPG_MD_JOINT_STEREO)
fprintf (stderr, "Layer II %s Psycho model=%d (Mode_Extension=%d)\n",
mode_names[header->mode], *psy, header->mode_ext);
else
fprintf (stderr, "Layer II %s Psy model %d \n", mode_names[header->mode],
*psy);
fprintf (stderr, "[De-emph:%s\tCopyright:%s\tOriginal:%s\tCRC:%s]\n",
((header->emphasis) ? "On" : "Off"),
((header->copyright) ? "Yes" : "No"),
((header->original) ? "Yes" : "No"),
((header->error_protection) ? "On" : "Off"));
fprintf (stderr, "[Padding:%s\tByte-swap:%s\tChanswap:%s\tDAB:%s]\n",
((glopts.usepadbit) ? "Normal" : "Off"),
((glopts.byteswap) ? "On" : "Off"),
((glopts.channelswap) ? "On" : "Off"),
((glopts.dab) ? "On" : "Off"));
if (glopts.vbr == TRUE)
fprintf (stderr, "VBR Enabled. Using MNR boost of %f\n", glopts.vbrlevel);
fprintf(stderr,"ATH adjustment %f\n",glopts.athlevel);
fprintf (stderr, "--------------------------------------------\n");
}
增添程序
…………
int gr;
FILE *out_txt=NULL;
unsigned char *outTXT=NULL;
out_txt=fopen("output.txt","w");
………………
fprintf(out_txt, "该音频声道数:%d\n", nch);
fprintf(out_txt, "观测第 %d 帧\n", frameNum);
fprintf(out_txt, "本帧比特预算:%d bits\n", adb);
fprintf(out_txt, "该帧比例因子和比特分配表如下:\n");
for (ch = 0; ch < nch; ch++) //逐声道输出
{
fprintf(out_txt, "--- 声道%2d ----\n", ch + 1);
for (sb = 0; sb < frame.sblimit; sb++) //各子带
{
fprintf(out_txt, "子带[%2d]比例因子:\t", sb + 1);
for (gr = 0; gr < 3; gr++)
{
fprintf(out_txt, "%2d\t", scalar[ch][gr][sb]);
}
fprintf(out_txt, "\n");
fprintf(out_txt, "子带[%2d]比特分配表:\t%2d\n", sb + 1, bit_alloc[ch][sb]);
fprintf(out_txt, "\n");
}
}
free(outTXT);
fclose(out_txt);
……
2.实验结果
文件:噪音,音乐,带噪音乐
音乐:
========== 基本信息 ==========
输入文件:music.wav
输出文件:music_192k.mp2
采样频率:44.1 kHz
输出文件码率:192 kbps
声道数:2
目前观测第 2 帧
本帧比特预算:5016 bits
========== 比例因子 ==========
------ 声道 1 ------
子带[ 1]: 33 35 35
子带[ 2]: 44 41 41
子带[ 3]: 47 46 46
子带[ 4]: 47 46 45
子带[ 5]: 46 44 47
子带[ 6]: 50 47 48
子带[ 7]: 46 49 49
子带[ 8]: 47 48 48
子带[ 9]: 49 48 48
子带[10]: 49 46 47
子带[11]: 48 47 48
子带[12]: 46 48 48
子带[13]: 49 48 48
子带[14]: 47 51 50
子带[15]: 49 49 48
子带[16]: 50 46 49
子带[17]: 51 50 48
子带[18]: 51 50 51
子带[19]: 52 49 51
子带[20]: 51 49 49
子带[21]: 49 51 50
子带[22]: 51 49 52
子带[23]: 48 49 50
子带[24]: 54 52 55
子带[25]: 54 56 54
子带[26]: 55 54 54
子带[27]: 53 54 53
子带[28]: 54 54 54
子带[29]: 54 56 53
子带[30]: 55 56 55
------ 声道 2 ------
子带[ 1]: 34 34 36
子带[ 2]: 43 43 44
子带[ 3]: 44 46 45
子带[ 4]: 45 47 47
子带[ 5]: 46 48 47
子带[ 6]: 47 49 51
子带[ 7]: 47 49 49
子带[ 8]: 49 48 48
子带[ 9]: 51 50 46
子带[10]: 49 49 49
子带[11]: 48 50 48
子带[12]: 50 48 48
子带[13]: 48 48 50
子带[14]: 47 49 50
子带[15]: 50 50 49
子带[16]: 48 49 49
子带[17]: 49 50 49
子带[18]: 50 50 50
子带[19]: 51 51 50
子带[20]: 51 49 50
子带[21]: 53 53 51
子带[22]: 53 50 50
子带[23]: 49 49 48
子带[24]: 51 51 53
子带[25]: 53 57 53
子带[26]: 57 53 56
子带[27]: 53 53 55
子带[28]: 54 53 54
子带[29]: 55 54 55
子带[30]: 56 57 54
========== 比特分配表 ==========
------ 声道 1 ------
子带[ 1]: 4
子带[ 2]: 4
子带[ 3]: 3
子带[ 4]: 5
子带[ 5]: 5
子带[ 6]: 5
子带[ 7]: 4
子带[ 8]: 5
子带[ 9]: 4
子带[10]: 5
子带[11]: 3
子带[12]: 6
子带[13]: 5
子带[14]: 3
子带[15]: 2
子带[16]: 2
子带[17]: 4
子带[18]: 4
子带[19]: 3
子带[20]: 3
子带[21]: 3
子带[22]: 3
子带[23]: 1
子带[24]: 1
子带[25]: 0
子带[26]: 1
子带[27]: 0
子带[28]: 1
子带[29]: 1
子带[30]: 0
------ 声道 2 ------
子带[ 1]: 3
子带[ 2]: 4
子带[ 3]: 4
子带[ 4]: 5
子带[ 5]: 5
子带[ 6]: 5
子带[ 7]: 4
子带[ 8]: 5
子带[ 9]: 4
子带[10]: 5
子带[11]: 3
子带[12]: 6
子带[13]: 5
子带[14]: 3
子带[15]: 2
子带[16]: 2
子带[17]: 4
子带[18]: 4
子带[19]: 3
子带[20]: 3
子带[21]: 3
子带[22]: 3
子带[23]: 1
子带[24]: 1
子带[25]: 0
子带[26]: 1
子带[27]: 0
子带[28]: 1
子带[29]: 1
子带[30]: 0
噪声:
========== 基本信息 ==========
输入文件:agwn.wav
输出文件:agwn_192k.mp2
采样频率:44.1 kHz
输出文件码率:192 kbps
声道数:1
目前观测第 2 帧
本帧比特预算:5016 bits
========== 比例因子 ==========
------ 声道 1 ------
子带[ 1]: 24 23 22
子带[ 2]: 25 23 25
子带[ 3]: 24 27 24
子带[ 4]: 24 26 25
子带[ 5]: 24 25 24
子带[ 6]: 27 25 25
子带[ 7]: 25 24 24
子带[ 8]: 25 24 24
子带[ 9]: 25 22 25
子带[10]: 25 25 25
子带[11]: 23 26 27
子带[12]: 25 24 24
子带[13]: 24 23 24
子带[14]: 23 24 24
子带[15]: 26 24 25
子带[16]: 24 25 23
子带[17]: 23 25 24
子带[18]: 25 23 23
子带[19]: 24 25 25
子带[20]: 23 26 24
子带[21]: 24 25 23
子带[22]: 24 25 23
子带[23]: 23 23 24
子带[24]: 26 25 24
子带[25]: 23 25 24
子带[26]: 24 23 26
子带[27]: 25 24 25
子带[28]: 24 26 23
子带[29]: 23 25 23
子带[30]: 26 24 22
========== 比特分配表 ==========
------ 声道 1 ------
子带[ 1]: 6
子带[ 2]: 5
子带[ 3]: 5
子带[ 4]: 7
子带[ 5]: 7
子带[ 6]: 7
子带[ 7]: 7
子带[ 8]: 7
子带[ 9]: 6
子带[10]: 7
子带[11]: 6
子带[12]: 6
子带[13]: 6
子带[14]: 6
子带[15]: 6
子带[16]: 4
子带[17]: 6
子带[18]: 6
子带[19]: 5
子带[20]: 5
子带[21]: 5
子带[22]: 5
子带[23]: 4
子带[24]: 1
子带[25]: 1
子带[26]: 1
子带[27]: 1
子带[28]: 1
子带[29]: 1
子带[30]: 0
带噪音乐
3.总结:
对于噪音和带噪音乐来说,各个子带所被分配到的比特数分布比较均匀
对于音乐而言,则不太均匀