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

📄 g726_32.c

📁 zigbee 飞思卡尔 音频传输 基于ucos的所有源码
💻 C
字号:
/*
 * 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
 */

/*
 * g721.c
 *
 * Description:
 *
 * g721_encoder(), g721_decoder()
 *
 * These routines comprise an implementation of the CCITT G.721 ADPCM
 * coding algorithm.  Essentially, this implementation is identical to
 * the bit level description except for a few deviations which
 * take advantage of work station attributes, such as hardware 2's
 * complement arithmetic and large memory.  Specifically, certain time
 * consuming operations such as multiplications are replaced
 * with lookup tables and software 2's complement operations are
 * replaced with hardware 2's complement.
 *
 * The deviation from the bit level specification (lookup tables)
 * preserves the bit level performance specifications.
 *
 * As outlined in the G.721 Recommendation, the algorithm is broken
 * down into modules.  Each section of code below is preceded by
 * the name of the module which it is implementing.
 *
 */

//#include <audiofmt/st.h>
#include "g72x.h"
//#include "g726private.h"
//#include <audiofmt/libst.h>

static int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
/*
 * Maps G.721 code word to reconstructed scale factor normalized log
 * magnitude values.
 */
static int	_dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
				425, 373, 323, 273, 213, 135, 4, -2048};

/* Maps G.721 code word to log of scale factor multiplier. */
static int	_witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
				1122, 355, 198, 112, 64, 41, 18, -12};
/*
 * Maps G.721 code words to a set of values whose long and short
 * term averages are computed and then compared to give an indication
 * how stationary (steady state) the signal is.
 */
static int	_fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
				0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};

/*
 * g726_32encoder()
 *
 * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
 * the resulting code. -1 is returned for unknown input coding value.
 */
int
g726_32encoder(int sl,struct g72x_state* state_ptr)
{
	short		sezi, se, sez;		/* ACCUM */
	short		d;			/* SUBTA */
	short		sr;			/* ADDB */
	short		y;			/* MIX */
	short		dqsez;			/* ADDC */
	short		dq, i;


	sl >>= 2;			/* 14-bit dynamic range */


	sezi = predictor_zero(state_ptr);
	sez = sezi >> 1;
	se = (sezi + predictor_pole(state_ptr)) >> 1;	/* estimated signal */

	d = sl - se;				/* estimation difference */

	/* quantize the prediction difference */
	y = step_size(state_ptr);		/* quantizer step size */
	i = quantize(d, y, qtab_721, 7);	/* i = ADPCM code */

	dq = reconstruct(i & 8, _dqlntab[i], y);	/* quantized est diff */

	sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;	/* reconst. signal */

	dqsez = sr + sez - se;			/* pole prediction diff. */

	update(y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);

	return (i);
}

/*
 * g726_32decoder()
 *
 * Description:
 *
 * Decodes a 4-bit code of G.721 encoded data of i and
 * returns the resulting linear PCM, A-law or u-law value.
 * return -1 for unknown out_coding value.
 */
int
g726_32decoder(int i, struct g72x_state* state_ptr)
{
	short		sezi, sei, sez, se;	/* ACCUM */
	short		y;			/* MIX */
	short		sr;			/* ADDB */
	short		dq;
	short		dqsez;

	i &= 0x0f;			/* mask to get proper bits */
	sezi = predictor_zero(state_ptr);
	sez = sezi >> 1;
	sei = sezi + predictor_pole(state_ptr);
	se = sei >> 1;			/* se = estimated signal */

	y = step_size(state_ptr);	/* dynamic quantizer step size */

	dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */

	sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq;	/* reconst. signal */

	dqsez = sr - se + sez;			/* pole prediction diff. */

	update(y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);

	return (sr << 2);	/* sr was 14-bit dynamic range */

}
/*
#include <stdio.h>

struct g72x_state st;
struct g72x_state *p;
struct g72x_state st1;
struct g72x_state *p1;

void main(){

FILE *fp,*fp2,*fp1;
//short data,dout;
int out_de,out_e,out_en[250] ;
int i,j;
int data[250] = {-14980, -9688,-24456,-17456,-14372,-11584,-18368,-14172,-12472,-10584,-14628,-12136,-11080,-18120, -6340,-20212, -5728,-27608, -6972,-12784, -9704,-18616, -6588,-27568, -5680, 27984,-28448, 21580,  8944,  7096,-29320,  9812, 17416,  3340,-19444,  3120, 28440, -2424,-10432, -4152,-26368, -8848, -1032,-12040,-14752,-16712,  8544,-21328, -3152,-25032, 18672,-30544,  9336, 30488, 29568, 24656, 21592, 20776,-26096, 15428,-31616, 10328,-14376,  3944,-17600, -2308,-15184, -6936, 16520, 26808, -3312, 19392,-23392,  4500,-18784, 32528,-28140, 14272, 14672, -6960, -9600,-24096, 28856, 16784, 23704, -8176, 10128, 20448,-19152, 25812, 25840,-13052, 13344, 26196, -2620, 10512,  2000,  4120,-14924,-30904,-32508,-23944,-31932,  3800, 16964, 10800, 27364,-24256, 12832,  -528, 20928, 16800,  4032,  5440, 24496, 15552,-15392, 25104, -9648,-16256, 22648,-27744, -1500,-17420,-24804,  1936,-27392, -2568, 20008,-32664,-19972,  -704, 10976, 25828,  9024, -6032, 24336,-13904, 21724,  4296,  4556,-22480, 26664, -1968,  2108,-32224,-24464,-29544,-10508,-14880,-22832, -8832,  3136,-23900, 29520,-15152, 12224, 21524,-13640, 15352, 32664, 12684, -8840, 21440, -4324, 17712, 14028, 21728,-19272, 17608,-29000, -7260,  6196,-15408, 24432, 28096,-10536,-23432,-13536, 17976,  7816,   664, 14432,-32380,-28048,-32312,   132, -8388,-16048,-10280, 29528, -1392,  1708,  1832, 16420, 29680,-32448, 10392, 19664, 24840,-31428,-20644,-21428, 30892,   504, 20952,  1936,-23336, 23584, -1632,-31224,-27356,-19100, 17072,  1504, 30472,-22768,-18472,-17884, -2908, 11840,  8252,  9288,-16480,  2944,-31056,  -256,  9140, 25120, -3968, -2980,-11392,  5624, 31524, 21384,-23572, -4356,-18164,  4488, 14220,-16624,-25088,-64,   -80,  -112,  -172,  -316,  -836, -2256, -5932};
p = &st;	
p1 = &st1;	
g72x_init_state(p1);
g72x_init_state(p);*/
/*	if ((fp = fopen("testport.wav", "rb")) == NULL) {
		printf ("Could not read.\n");
		return;
	}
	if ((fp2 = fopen("g721_out2", "wb")) == NULL) {
		printf ("Could not create \"tstseq1.wav\".\n");
		return;
	}
	for(i = 0;i<20;i++){
		for(j = 0;j<80;j++){
		fread(&data,2,1,fp);
		out_e = (int)data;
		out_en[j] = g721_encoder(out_e,p);
		}
		for(j = 0;j<80;j++){
		out_de = g721_decoder(out_en[j],p1);
		dout = (short)out_de;
		fwrite(&dout,2,1,fp2);
		}
	}
fclose(fp);
fclose(fp2);
}
*//*
for(i=0;i<250;i++){
	out_e = g721_encoder(data[i],p);
	out_en[i] = g721_decoder(out_e,p1);
	printf("%d,",out_en[i]);
	}
}*/

⌨️ 快捷键说明

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