g726_24.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:4k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*
  2.  * This source code is a product of Sun Microsystems, Inc. and is provided
  3.  * for unrestricted use.  Users may copy or modify this source code without
  4.  * charge.
  5.  *
  6.  * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
  7.  * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  8.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  9.  *
  10.  * Sun source code is provided with no support and without any obligation on
  11.  * the part of Sun Microsystems, Inc. to assist in its use, correction,
  12.  * modification or enhancement.
  13.  *
  14.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  15.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
  16.  * OR ANY PART THEREOF.
  17.  *
  18.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  19.  * or profits or other special, indirect and consequential damages, even if
  20.  * Sun has been advised of the possibility of such damages.
  21.  *
  22.  * Sun Microsystems, Inc.
  23.  * 2550 Garcia Avenue
  24.  * Mountain View, California  94043
  25.  */
  26. /*
  27.  * g726_24.c
  28.  *
  29.  * Description:
  30.  *
  31.  * g723_24_encoder(), g723_24_decoder()
  32.  *
  33.  * These routines comprise an implementation of the CCITT G.723 24 Kbps
  34.  * ADPCM coding algorithm.  Essentially, this implementation is identical to
  35.  * the bit level description except for a few deviations which take advantage
  36.  * of workstation attributes, such as hardware 2's complement arithmetic.
  37.  *
  38.  * The ITU-T G.726 coder is an adaptive differential pulse code modulation
  39.  * (ADPCM) waveform coding algorithm, suitable for coding of digitized
  40.  * telephone bandwidth (0.3-3.4 kHz) speech or audio signals sampled at 8 kHz.
  41.  * This coder operates on a sample-by-sample basis. Input samples may be 
  42.  * represented in linear PCM or companded 8-bit G.711 (m-law/A-law) formats
  43.  * (i.e., 64 kbps). For 32 kbps operation, each sample is converted into a
  44.  * 4-bit quantized difference signal resulting in a compression ratio of 
  45.  * 2:1 over the G.711 format. For 24 kbps 40 kbps operation, the quantized
  46.  * difference signal is 3 bits and 5 bits, respectively.
  47.  *
  48.  * $Log: g726_24.c,v $
  49.  * Revision 1.4  2002/11/20 04:29:13  robertj
  50.  * Included optimisations for G.711 and G.726 codecs, thanks Ted Szoczei
  51.  *
  52.  * Revision 1.1  2002/02/11 23:24:23  robertj
  53.  * Updated to openH323 v1.8.0
  54.  *
  55.  * Revision 1.2  2002/02/10 21:14:54  dereks
  56.  * Add cvs log history to head of the file.
  57.  * Ensure file is terminated by a newline.
  58.  *
  59.  *
  60.  *
  61.  */
  62. #include "g72x.h"
  63. #include "private.h"
  64. /*
  65.  * Maps G.723_24 code word to reconstructed scale factor normalized log
  66.  * magnitude values.
  67.  */
  68. static const short _dqlntab[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048};
  69. /* Maps G.723_24 code word to log of scale factor multiplier. */
  70. static const short _witab[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128};
  71. /*
  72.  * Maps G.723_24 code words to a set of values whose long and short
  73.  * term averages are computed and then compared to give an indication
  74.  * how stationary (steady state) the signal is.
  75.  */
  76. static const short _fitab[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0};
  77. //static int qtab_723_24[3] = {8, 218, 331};
  78. /*
  79.  * g723_24_decoder()
  80.  *
  81.  * Decodes a 3-bit CCITT G.723_24 ADPCM code and returns
  82.  * the resulting 16-bit linear PCM, A-law or u-law sample value.
  83.  * -1 is returned if the output coding is unknown.
  84.  */
  85. int
  86. g726_24_decoder(
  87. int i,
  88. g726_state *state_ptr)
  89. {
  90. int sezi;
  91. int sez; /* ACCUM */
  92. int sei;
  93. int se;
  94. int y; /* MIX */
  95. int dq;
  96. int sr; /* ADDB */
  97. int dqsez;
  98. i &= 0x07; /* mask to get proper bits */
  99. sezi = predictor_zero(state_ptr);
  100. sez = sezi >> 1;
  101. sei = sezi + predictor_pole(state_ptr);
  102. se = sei >> 1; /* se = estimated signal */
  103. y = step_size(state_ptr); /* adaptive quantizer step size */
  104. dq = reconstruct(i & 0x04, _dqlntab[i], y); /* unquantize pred diff */
  105. sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
  106. dqsez = sr - se + sez; /* pole prediction diff. */
  107. update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
  108. return (sr << 2); /* sr was of 14-bit dynamic range */
  109. }