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

📄 sadeco.cpp

📁 这是G.723和G.729的音频编解码的源代码
💻 CPP
字号:

#include "stdafx.h"
#include "sactable.h"
#include "DBitstream.h"
#include "sad.h"

#define ESCAPE_INDEX                    102


SAD::SAD(DBitStream *InStream)
{
	InputStream=InStream;
	zerorun=0;
}

int SAD::decode_a_symbol(const int *cumul_freq)
{	register int i;
	
	SADlength = high - low + 1;
	cum = (-1 + (code_value - low + 1) * cumul_freq[0]) / SADlength;
	for (i = 1; cumul_freq[i] > cum; i++);
	sacindex=i;
	high = low - 1 + (SADlength * cumul_freq[sacindex-1]) / cumul_freq[0];
	low += (SADlength * cumul_freq[sacindex]) / cumul_freq[0];

	for ( ; ; ) 
		{if (high < q2)
			{low += low;
			 high +=(high+1);
			 bit_out_psc_layer(); 
			 code_value += (code_value + bit);
			 continue;
			}
		 else if (low >= q2)
			{low +=( low-65536); 
			 high +=(high-top);
			 bit_out_psc_layer(); 
			 code_value += (code_value -65536+ bit);
			 continue;
			}
		 else if (low >= q1 && high < q3)
			{low += (low-q2); 
			 high+= (high-32767);
			 bit_out_psc_layer(); 
			 code_value += (code_value -q2+ bit);
			 continue;
			}
		 else
			break;
	}
	return (sacindex-1);
}
 
void SAD::DecodeReset( )
{
	int i;
	zerorun = 0;			/* clear consecutive zero's counter */
	code_value = 0;
	low = 0;
	high = top;
	for (i = 1;   i <= 16;   i++)
		{bit_out_psc_layer(); 
		 code_value = 2 * code_value + bit;
		}
}

_inline void SAD::bit_out_psc_layer()
{
	if (InputStream->ReadVarible(17)!=1)
		{ // check for startcode in Arithmetic Decoder FIFO
		 bit = InputStream->GetOneBit();
		 if(zerorun > 13) 
			{// if number of consecutive zeros = 14
			 if (!bit)
				zerorun = 1;		
             else 
				{// if there is a 'stuffing bit present 
				 bit = InputStream->GetOneBit(); 	// overwrite the last bit
				 zerorun = !bit;	  	// zerorun=1 if bit is a '0'
				}
			}
		 else 
			{ // if consecutive zero's not exceeded 14
			 if (!bit)
				zerorun++;
			 else
				zerorun = 0;
			}
		} // end of if ReadVarible(17)!=1
	else 
		bit = 0;
}

int SAD::GetIntraBlock(short *QCoeff)
{
	int position=0;
	int TCOEF_index, symbol_word;
	int last=0,i=1;
    RunCoef DCTcoef;

	while (!last)
		{	/* while there are DCT coefficients remaining */
		 position++;	/* coefficient counter relates to Coeff. model */
		 TCOEF_index = DecodeTCoef(position,TRUE);//Intra

		 if (TCOEF_index == ESCAPE_INDEX)/* ESCAPE code encountered */
			 DCTcoef = Decode_Escape_Char(TRUE, &last);//Intra
         else 
			{symbol_word = tcoeftab[TCOEF_index];
			 DCTcoef = vlc_word_decode(symbol_word,&last);
			}

		 i += DCTcoef.run;
		 if (DCTcoef.sign)
			 QCoeff[i++]=-DCTcoef.val;
		 else
			 QCoeff[i++]=DCTcoef.val;
		}	
  return TRUE;
}

RunCoef SAD::vlc_word_decode(int symbol_word, int *last)
{
	int sign_index;
	RunCoef DCTcoef;

	*last = (symbol_word >> 12) & 01;
 	DCTcoef.run = (symbol_word >> 4) & 255; 
	DCTcoef.val = (symbol_word) & 15;
	sign_index =decode_a_symbol(cumf_SIGN);	
	DCTcoef.sign = signtab[sign_index];
	
	return (DCTcoef);
} 

RunCoef SAD::Decode_Escape_Char(BOOL intra, int *last)
{
	int last_index, run, run_index, level, level_index;
	RunCoef DCTcoef;

	if (intra) 
		{last_index =decode_a_symbol(cumf_LAST_intra);
		 *last = last_intratab[last_index];
		}
	else 
		{last_index =decode_a_symbol(cumf_LAST);
		 *last = lasttab[last_index];
		}

	if (intra) 
		run_index =decode_a_symbol(cumf_RUN_intra);
	else
		run_index =decode_a_symbol(cumf_RUN);

	run = runtab[run_index];
    DCTcoef.run = run;

	if (intra)
		level_index =decode_a_symbol(cumf_LEVEL_intra);
	else
		level_index =decode_a_symbol(cumf_LEVEL);

    level = leveltab[level_index];

	if (level >128) 
		level -=256;
	if (level < 0) 
		{DCTcoef.sign = 1;
		 DCTcoef.val =abs(level);
		}
	else
		{DCTcoef.sign = 0;
		 DCTcoef.val = level;
		}
	return (DCTcoef);
}

int SAD::DecodeTCoef(int position,BOOL intra)
{
	int index;

	switch (position)
		{case 1:
			if (intra) 
				index =decode_a_symbol(cumf_TCOEF1_intra);
			else 
				index =decode_a_symbol(cumf_TCOEF1); 
			break; 
		 case 2:
			if (intra) 
				index =decode_a_symbol(cumf_TCOEF2_intra);
			else
				index =decode_a_symbol(cumf_TCOEF2);
			break; 
		 case 3:
			if (intra) 
				index =decode_a_symbol(cumf_TCOEF3_intra);
			else
				index =decode_a_symbol(cumf_TCOEF3);
			break; 
		 default: 
			if (intra) 
				index =decode_a_symbol(cumf_TCOEFr_intra);
			else
				index =decode_a_symbol(cumf_TCOEFr);
			break; 
		}
	return (index);
}

int SAD::GetInterBlock(short *QCoeff)
{
	int position=0;
	int TCOEF_index, symbol_word;
	int last=0,i=0;
    RunCoef DCTcoef;

	while (!last)
		{	/* while there are DCT coefficients remaining */
		 position++;	/* coefficient counter relates to Coeff. model */
		 TCOEF_index = DecodeTCoef(position,FALSE);//Inter

		 if (TCOEF_index == ESCAPE_INDEX)/* ESCAPE code encountered */
			 DCTcoef = Decode_Escape_Char(FALSE, &last);//Inter
         else 
			{symbol_word = tcoeftab[TCOEF_index];
			 DCTcoef = vlc_word_decode(symbol_word,&last);
			}

		 i += DCTcoef.run;
		 if (DCTcoef.sign)
			 QCoeff[i++]=-DCTcoef.val;
		 else
			 QCoeff[i++]=DCTcoef.val;
		}	
  return TRUE;
}

⌨️ 快捷键说明

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