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

📄 g72x.c

📁 SUN的ADPCM压缩解压程序,包括G.721,G.723,其中G.723又包括16bit,24bit,40bit的压缩解压
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * This source code is a product of Sun Microsystems, Inc. and is provided
 * for unrestricted use.  Users may copy or modify this source code without
 * charge.
 *
 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
 *
 * Sun source code is provided with no support and without any obligation on
 * the part of Sun Microsystems, Inc. to assist in its use, correction,
 * modification or enhancement.
 *
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
 * OR ANY PART THEREOF.
 *
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
 * or profits or other special, indirect and consequential damages, even if
 * Sun has been advised of the possibility of such damages.
 *
 * Sun Microsystems, Inc.
 * 2550 Garcia Avenue
 * Mountain View, California  94043
 */

/*
 * g72x.c
 *
 * Common routines for G.721 and G.723 conversions.
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "g72x.h"
#include "g72x_priv.h"

static 
short power2 [15] = 
{	1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
	0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000
} ;

/*
 * quan()
 *
 * quantizes the input val against the table of size short integers.
 * It returns i if table[i - 1] <= val < table[i].
 *
 * Using linear search for simple coding.
 */
static 
int quan (int val, short *table, int size)
{
	int		i;

	for (i = 0; i < size; i++)
		if (val < *table++)
			break;
	return (i);
}

/*
 * fmult()
 *
 * returns the integer product of the 14-bit integer "an" and
 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
 */
static 
int fmult (int an, int srn)
{
	short		anmag, anexp, anmant;
	short		wanexp, wanmant;
	short		retval;

	anmag = (an > 0) ? an : ((-an) & 0x1FFF);
	anexp = quan(anmag, power2, 15) - 6;
	anmant = (anmag == 0) ? 32 :
	    (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
	wanexp = anexp + ((srn >> 6) & 0xF) - 13;

	/*
	** The original was :
	**		wanmant = (anmant * (srn & 0x37) + 0x30) >> 4 ;
	** but could see no valid reason for the + 0x30.
	** Removed it and it improved the SNR of the codec.
	*/

	wanmant = (anmant * (srn & 0x37)) >> 4 ;
	
	retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
	    (wanmant >> -wanexp);

	return (((an ^ srn) < 0) ? -retval : retval);
}

/*
 * private_init_state()
 *
 * This routine initializes and/or resets the G72x_PRIVATE structure
 * pointed to by 'state_ptr'.
 * All the initial state values are specified in the CCITT G.721 document.
 */
void private_init_state (G72x_STATE *state_ptr)
{
	int		cnta;

	state_ptr->yl = 34816;
	state_ptr->yu = 544;
	state_ptr->dms = 0;
	state_ptr->dml = 0;
	state_ptr->ap = 0;
	for (cnta = 0; cnta < 2; cnta++) {
		state_ptr->a[cnta] = 0;
		state_ptr->pk[cnta] = 0;
		state_ptr->sr[cnta] = 32;
	}
	for (cnta = 0; cnta < 6; cnta++) {
		state_ptr->b[cnta] = 0;
		state_ptr->dq[cnta] = 32;
	}
	state_ptr->td = 0;
}	/* private_init_state */

int g72x_reader_init (G72x_DATA *data, int codec)
{	G72x_STATE *pstate ;

	if (sizeof (data->private) < sizeof (G72x_STATE))
	{	/* This is for safety only. */
		return 1 ;
		} ;

	memset (data, 0, sizeof (G72x_DATA)) ;
	
	pstate = (G72x_STATE*) data->private ;
	private_init_state (pstate) ;
		
	pstate->encoder = NULL ;
	
	switch (codec)
	{	case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
				pstate->decoder = g723_16_decoder ;
				data->blocksize = G723_16_BYTES_PER_BLOCK ;
				data->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
				pstate->codec_bits = 2 ;
				break ;
				
		case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */ 
				pstate->decoder = g723_24_decoder ;
				data->blocksize = G723_24_BYTES_PER_BLOCK ;
				data->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
				pstate->codec_bits = 3 ;
				break ;
				
		case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
				pstate->decoder = g721_decoder ;
				data->blocksize = G721_32_BYTES_PER_BLOCK ;
				data->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
				pstate->codec_bits = 4 ;
				break ;
				
		case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
				pstate->decoder = g723_40_decoder ;
				data->blocksize = G721_40_BYTES_PER_BLOCK ;
				data->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
				pstate->codec_bits = 5 ;
				break ;
				
		default : return 1 ;
		} ;

	return 0 ;
}	/* g72x_reader_init */

int g72x_writer_init (G72x_DATA *data, int codec)
{	G72x_STATE *pstate ;

	if (sizeof (data->private) < sizeof (G72x_STATE))
	{	/* This is for safety only. Gets optimised out. */
		return 1 ;
		} ;

	memset (data, 0, sizeof (G72x_DATA)) ;
	
	pstate = (G72x_STATE*) data->private ;
	private_init_state (pstate) ;
		
	pstate->decoder = NULL ;
	
	switch (codec)
	{	case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
				pstate->encoder = g723_16_encoder ;
				data->blocksize = G723_16_BYTES_PER_BLOCK ;
				data->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
				pstate->codec_bits = 2 ;
				break ;
				
		case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */ 
				pstate->encoder = g723_24_encoder ;
				data->blocksize = G723_24_BYTES_PER_BLOCK ;
				data->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
				pstate->codec_bits = 3 ;
				break ;
				
		case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
				pstate->encoder = g721_encoder ;
				data->blocksize = G721_32_BYTES_PER_BLOCK ;
				data->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
				pstate->codec_bits = 4 ;
				break ;
				
		case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
				pstate->encoder = g723_40_encoder ;
				data->blocksize = G721_40_BYTES_PER_BLOCK ;
				data->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
				pstate->codec_bits = 5 ;
				break ;
				
		default : return 1 ;
		} ;

	return 0 ;
}	/* g72x_writer_init */

int unpack_bytes (G72x_DATA *data, int bits)
{	unsigned int    in_buffer = 0 ;
	unsigned char	in_byte ;
	int				k, in_bits = 0, bindex = 0 ;
	
	for (k = 0 ; bindex <= data->blocksize && k < G72x_BLOCK_SIZE ; k++)
	{	if (in_bits < bits) 
		{	in_byte = data->block [bindex++] ;

			in_buffer |= (in_byte << in_bits);
			in_bits += 8;
			}
		data->samples [k] = in_buffer & ((1 << bits) - 1);
		in_buffer >>= bits;
		in_bits -= bits;
		} ;
		
	return k ;
} /* unpack_bytes */

int g72x_decode_block (G72x_DATA *data)
{	G72x_STATE *pstate ;
	int	k, count ;
	
	pstate = (G72x_STATE*) data->private ;
	
	count = unpack_bytes (data, pstate->codec_bits) ;
	
	for (k = 0 ; k < count ; k++)
		data->samples [k] = pstate->decoder (data->samples [k], pstate) ;
	
	return 0 ;
}	/* g72x_decode_block */

int pack_bytes (G72x_DATA *data, int bits)
{
	unsigned int	out_buffer = 0 ;
	int				k, bindex = 0, out_bits = 0 ;
	unsigned char	out_byte ;

	for (k = 0 ; k < G72x_BLOCK_SIZE ; k++)
	{	out_buffer |= (data->samples [k] << out_bits) ;
		out_bits += bits ;
		if (out_bits >= 8) 
		{	out_byte = out_buffer & 0xFF ;
			out_bits -= 8 ;
			out_buffer >>= 8 ;
			data->block [bindex++] = out_byte ;
			}
		} ;

	return bindex ;
} /* pack_bytes */

int g72x_encode_block (G72x_DATA *data)
{	G72x_STATE *pstate ;
	int k, count ;

	pstate = (G72x_STATE*) data->private ;
	
	for (k = 0 ; k < data->samplesperblock ; k++)
		data->samples [k] = pstate->encoder (data->samples [k], pstate) ;
	
	count = pack_bytes (data, pstate->codec_bits) ;
	
	return count ;
}	/* g72x_encode_block */

/*
 * predictor_zero()
 *
 * computes the estimated signal from 6-zero predictor.
 *
 */
int  predictor_zero (G72x_STATE *state_ptr)
{
	int		i;
	int		sezi;

	sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
	for (i = 1; i < 6; i++)			/* ACCUM */
		sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
	return (sezi);
}
/*
 * predictor_pole()
 *
 * computes the estimated signal from 2-pole predictor.
 *
 */
int  predictor_pole(G72x_STATE *state_ptr)
{
	return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +

⌨️ 快捷键说明

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