⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ms.c

📁 AAC音频解码算法程序
💻 C
📖 第 1 页 / 共 2 页
字号:

			if (block_type[chanNum] == ONLY_SHORT_WINDOW) {
				p_use_ms = p_chpo_short[1][w].use_ms;
			} else {
				p_use_ms = p_chpo_long[1].use_ms;
			}

			j = w*windowLength;       //j用来作为谱序列的计数变量

			/* Only compute energy up to max_sfb */
			for(sfb=0; sfb< quantInfo[chanNum].max_sfb; sfb++ ) {
				/* calculate scale factor band energy */
				int width,i;
				energy[chanNum][bandNumber] = 0.0;
				width=sfb_width_table[chanNum][sfb];
				for(i=offset; i<(offset+width); i++ ) {
					if (((p_use_ms[sfb]||(use_ms==1)) && (chanNum == 0))&&(use_ms!=-1))
						dtmp = (spectral_line_vector[0][j]+spectral_line_vector[1][j])*0.5;
					else if (((p_use_ms[sfb]||(use_ms==1)) && (chanNum == 1))&&(use_ms!=-1))
						dtmp = (spectral_line_vector[0][j]-spectral_line_vector[1][j])*0.5;
					else
						dtmp = spectral_line_vector[chanNum][j];
					j++;
					energy[chanNum][bandNumber] += dtmp*dtmp;     //累加一个sfb中的能量。
				}
				energy[chanNum][bandNumber] = energy[chanNum][bandNumber] / width;  //平均能量
				bandNumber++;
				offset+=width;
			}  
		}
	}  /* for (chanNum... */
	//代码还可以优化,要考虑多通道时的改动。
}

/* Perform MS encoding.  Spectrum is non-interleaved.  */
/* This would be a lot simpler on interleaved spectral data */
void MSEncode(double *spectrum[MAX_TIME_CHANNELS],   /* array of pointers to spectral data */
			  Ch_Info *channelInfo,                  /* Pointer to Ch_Info */
			  int sfb_offset_table[][MAX_SCFAC_BANDS+1],
			  enum WINDOW_TYPE block_type[MAX_TIME_CHANNELS], /* Block type */
			  AACQuantInfo* quantInfo,               /* Quant info */
			  int numberOfChannels) {                /* Number of channels */

	int chanNum;
	int sfbNum;
	int lineNum;
	double sum,diff;

	/* Look for channel_pair_elements */
	for (chanNum=0;chanNum<numberOfChannels;chanNum++) {
		if (channelInfo[chanNum].present) {
			if ((channelInfo[chanNum].cpe)&&(channelInfo[chanNum].ch_is_left)) {
				int leftChan=chanNum;
				int rightChan=channelInfo[chanNum].paired_ch;
				channelInfo[leftChan].ms_info.is_present=0;    

				/* Perform MS if block_types are the same */
				if (block_type[leftChan]==block_type[rightChan]) { 

					int numGroups;
					int maxSfb;
					int g,b,w,line_offset;
					int startWindow,stopWindow;
					IS_Info *isInfo;
					MS_Info *msInfo;

					channelInfo[leftChan].common_window = 1;  /* Use common window */
					channelInfo[leftChan].ms_info.is_present=1;
                    w = 0;

					numGroups = quantInfo[leftChan].num_window_groups;
					maxSfb = quantInfo[leftChan].max_sfb;

					/* Determine which bands should be enabled */
					/* Right now, simply enable bands which do not use intensity stereo */
					isInfo = &(channelInfo[rightChan].is_info);
					msInfo = &(channelInfo[leftChan].ms_info);
					for (g=0;g<numGroups;g++) {
						for (sfbNum=0;sfbNum<maxSfb;sfbNum++) {
							b = g*maxSfb+sfbNum;
							msInfo->ms_used[b] = ( (!isInfo->is_used[b])||(!isInfo->is_present) );
						}
					}
					
					/* Perform sum and differencing on bands in which ms_used flag */
					/* has been set. */
					line_offset=0;
					startWindow = 0;
					for (g=0;g<numGroups;g++) {
						int numWindows = quantInfo[leftChan].window_group_length[g];
						stopWindow = startWindow + numWindows;
						for (sfbNum=0;sfbNum<maxSfb;sfbNum++) {
							/* Enable MS mask */
							if (msInfo->ms_used[g*maxSfb+sfbNum]) {
								for (w=startWindow;w<stopWindow;w++) {
									for (lineNum=sfb_offset_table[leftChan][sfbNum];
									lineNum<sfb_offset_table[leftChan][sfbNum+1];
									lineNum++) {
										line_offset = w*BLOCK_LEN_SHORT;
										sum=spectrum[leftChan][line_offset+lineNum]+spectrum[rightChan][line_offset+lineNum];
										diff=spectrum[leftChan][line_offset+lineNum]-spectrum[rightChan][line_offset+lineNum];
										spectrum[leftChan][line_offset+lineNum] = 0.5 * sum;
										spectrum[rightChan][line_offset+lineNum] = 0.5 * diff;
									}  /* for (lineNum... */
								}  /* for (w=... */
							}
						}  /* for (sfbNum... */
						startWindow = stopWindow;
					} /* for (g... */
				}  /* if (block_type... */
			}
		}  /* if (channelInfo[chanNum].present */
	}  /* for (chanNum... */
}   


/* Perform MS encoding.  Spectrum is non-interleaved.  */
/* This would be a lot simpler on interleaved spectral data */
void MSEncodeSwitch(double *spectrum[MAX_TIME_CHANNELS],   /* array of pointers to spectral data */
	      Ch_Info *channelInfo,                  /* Pointer to Ch_Info */
	      int sfb_offset_table[][MAX_SCFAC_BANDS+1],
	      enum WINDOW_TYPE block_type[MAX_TIME_CHANNELS], /* Block type */
	      AACQuantInfo* quantInfo,               /* Quant info */
	      int numberOfChannels)                  /* Number of channels */
{
	int chanNum;
	int sfbNum;
	int lineNum;
	double sum,diff;

	/* Look for channel_pair_elements */
//	for (chanNum=0;chanNum<numberOfChannels;chanNum++) {
	chanNum = 0;
		if (channelInfo[0].ms_info.is_present) {
				int leftChan=chanNum;
				int rightChan=channelInfo[chanNum].paired_ch;


					int numGroups;
					int maxSfb;
					int g,b,w,line_offset;
					int startWindow,stopWindow;
					IS_Info *isInfo;
					MS_Info *msInfo;

					channelInfo[leftChan].common_window = 1;  /* Use common window */
					channelInfo[leftChan].ms_info.is_present=1;

					numGroups = quantInfo[leftChan].num_window_groups;
					maxSfb = quantInfo[leftChan].max_sfb;
					w=0;

					/* Determine which bands should be enabled */
					/* Right now, simply enable bands which do not use intensity stereo */
					isInfo = &(channelInfo[rightChan].is_info);
					msInfo = &(channelInfo[leftChan].ms_info);
#if 1
					for (g=0;g<numGroups;g++) {
						for (sfbNum=0;sfbNum<maxSfb;sfbNum++) {
							b = g*maxSfb+sfbNum;
							msInfo->ms_used[b] = (((!isInfo->is_used[b])||(!isInfo->is_present))&&msInfo->ms_used[b]);
						}
					}
#endif
					/* Perform sum and differencing on bands in which ms_used flag */
					/* has been set. */
					line_offset=0;
					startWindow = 0;
					for (g=0;g<numGroups;g++) {
						int numWindows = quantInfo[leftChan].window_group_length[g];
						stopWindow = startWindow + numWindows;
						for (sfbNum=0;sfbNum<maxSfb;sfbNum++) {
							/* Enable MS mask */
							if (msInfo->ms_used[g*maxSfb+sfbNum]) {
								for (w=startWindow;w<stopWindow;w++) {
									for (lineNum=sfb_offset_table[leftChan][sfbNum];
									lineNum<sfb_offset_table[leftChan][sfbNum+1];
									lineNum++) {
										line_offset = w*BLOCK_LEN_SHORT;
										sum=spectrum[leftChan][line_offset+lineNum]+spectrum[rightChan][line_offset+lineNum];
										diff=spectrum[leftChan][line_offset+lineNum]-spectrum[rightChan][line_offset+lineNum];
										spectrum[leftChan][line_offset+lineNum] = 0.5 * sum;
										spectrum[rightChan][line_offset+lineNum] = 0.5 * diff;
									}  /* for (lineNum... */
								}  /* for (w=... */
							}
						}  /* for (sfbNum... */
						startWindow = stopWindow;
					} /* for (g... */
				}  /* if (block_type... */
//	}  /* for (chanNum... */
}


void MSReconstruct(double *spectrum[MAX_TIME_CHANNELS],   /* array of pointers to spectral data */
		   Ch_Info *channelInfo,                  /* Pointer to Ch_Info */
		   int sfb_offset_table[][MAX_SCFAC_BANDS+1],
		   enum WINDOW_TYPE block_type[MAX_TIME_CHANNELS], /* Block type */
		   AACQuantInfo* quantInfo,               /* Quant info */
		   int numberOfChannels)                 /* Number of channels */
{
	int chanNum;
	int sfbNum;
	int lineNum;
	double sum,diff;

	/* Look for channel_pair_elements */
	for (chanNum=0;chanNum<numberOfChannels;chanNum++) {
		if (channelInfo[chanNum].present) {
			if ((channelInfo[chanNum].cpe)&&(channelInfo[chanNum].ch_is_left)) {
				int leftChan=chanNum;
				int rightChan=channelInfo[chanNum].paired_ch;

				MS_Info *msInfo;
				msInfo = &(channelInfo[leftChan].ms_info);
				if (msInfo->is_present) {
					int numGroups = quantInfo[leftChan].num_window_groups;
					int maxSfb = quantInfo[leftChan].max_sfb;
					int g,w,line_offset;
					int startWindow,stopWindow;
					w=0;

					/* Perform sum and differencing on bands in which ms_used flag */
					/* has been set. */
					line_offset=0;
					startWindow = 0;
					for (g=0;g<numGroups;g++) {
						int numWindows = quantInfo[leftChan].window_group_length[g];
						stopWindow = startWindow + numWindows;
						for (sfbNum=0;sfbNum<maxSfb;sfbNum++) {
							/* Enable MS mask */
							if ((msInfo->ms_used[g*maxSfb+sfbNum]) || (msInfo->is_present==2)) {
								for (w=startWindow;w<stopWindow;w++) {
									line_offset = w*BLOCK_LEN_SHORT;
									for (lineNum=sfb_offset_table[leftChan][sfbNum];
									lineNum<sfb_offset_table[leftChan][sfbNum+1];
									lineNum++) {
										sum=spectrum[leftChan][line_offset+lineNum];
										diff=spectrum[rightChan][line_offset+lineNum];
										spectrum[leftChan][line_offset+lineNum] = (sum+diff);
										spectrum[rightChan][line_offset+lineNum] = (sum-diff);
									}  /* for (lineNum... */
								}  /* for (w=start... */
							}  /* if (msInfo... */
						}  /* for (sfbNum... */
						startWindow = stopWindow;
					} /* for (g... */
				}  /* if (ms_info.is_present... */
			}  /* if (channelInfo... */
		}  /* if (channelInfo[chanNum].present */
	}  /* for (chanNum... */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -