g726.h

Go to the documentation of this file.
00001 /*
00002  * SpanDSP - a series of DSP components for telephony
00003  *
00004  * g726.h - ITU G.726 codec.
00005  *
00006  * Written by Steve Underwood <steveu@coppice.org>
00007  *
00008  * Copyright (C) 2006 Steve Underwood
00009  *
00010  * All rights reserved.
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License version 2, as
00014  * published by the Free Software Foundation.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024  *
00025  * Based on G.721/G.723 code which is:
00026  *
00027  * This source code is a product of Sun Microsystems, Inc. and is provided
00028  * for unrestricted use.  Users may copy or modify this source code without
00029  * charge.
00030  *
00031  * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
00032  * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00033  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00034  *
00035  * Sun source code is provided with no support and without any obligation on
00036  * the part of Sun Microsystems, Inc. to assist in its use, correction,
00037  * modification or enhancement.
00038  *
00039  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00040  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
00041  * OR ANY PART THEREOF.
00042  *
00043  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00044  * or profits or other special, indirect and consequential damages, even if
00045  * Sun has been advised of the possibility of such damages.
00046  *
00047  * Sun Microsystems, Inc.
00048  * 2550 Garcia Avenue
00049  * Mountain View, California  94043
00050  *
00051  * $Id: g726.h,v 1.16 2007/04/08 08:16:17 steveu Exp $
00052  */
00053 
00054 /*! \file */
00055 
00056 #if !defined(_SPANDSP_G726_H_)
00057 #define _SPANDSP_G726_H_
00058 
00059 /*! \page g726_page G.726 encoding and decoding
00060 \section g726_page_sec_1 What does it do?
00061 
00062 The G.726 module is a bit exact implementation of the full ITU G.726 specification.
00063 It supports:
00064     - 16 kbps, 24kbps, 32kbps, and 40kbps operation.
00065     - Tandem adjustment, for interworking with A-law and u-law.
00066     - Annex A support, for use in environments not using A-law or u-law.
00067 
00068 It passes the ITU tests.
00069 
00070 \section g726_page_sec_2 How does it work?
00071 ???.
00072 */
00073 
00074 enum
00075 {
00076     G726_ENCODING_LINEAR = 0,   /* Interworking with 16 bit signed linear */
00077     G726_ENCODING_ULAW,         /* Interworking with u-law */
00078     G726_ENCODING_ALAW          /* Interworking with A-law */
00079 };
00080 
00081 enum
00082 {
00083     G726_PACKING_NONE = 0,
00084     G726_PACKING_LEFT = 1,
00085     G726_PACKING_RIGHT = 2
00086 };
00087 
00088 struct g726_state_s;
00089 
00090 typedef int16_t (*g726_decoder_func_t)(struct g726_state_s *s, uint8_t code);
00091 
00092 typedef uint8_t (*g726_encoder_func_t)(struct g726_state_s *s, int16_t amp);
00093 
00094 /*
00095  * The following is the definition of the state structure
00096  * used by the G.726 encoder and decoder to preserve their internal
00097  * state between successive calls.  The meanings of the majority
00098  * of the state structure fields are explained in detail in the
00099  * CCITT Recommendation G.721.  The field names are essentially indentical
00100  * to variable names in the bit level description of the coding algorithm
00101  * included in this Recommendation.
00102  */
00103 typedef struct g726_state_s
00104 {
00105     /*! The bit rate */
00106     int rate;
00107     /*! The external coding, for tandem operation */
00108     int ext_coding;
00109     /*! The number of bits per sample */
00110     unsigned int bits_per_sample;
00111     /*! One fo the G.726_PACKING_xxx options */
00112     int packing;
00113 
00114     /*! Locked or steady state step size multiplier. */
00115     int32_t yl;
00116     /*! Unlocked or non-steady state step size multiplier. */
00117     int16_t yu;
00118     /*! int16_t term energy estimate. */
00119     int16_t dms;
00120     /*! Long term energy estimate. */
00121     int16_t dml;
00122     /*! Linear weighting coefficient of 'yl' and 'yu'. */
00123     int16_t ap;
00124     
00125     /*! Coefficients of pole portion of prediction filter. */
00126     int16_t a[2];
00127     /*! Coefficients of zero portion of prediction filter. */
00128     int16_t b[6];
00129     /*! Signs of previous two samples of a partially reconstructed signal. */
00130     int16_t pk[2];
00131     /*! Previous 6 samples of the quantized difference signal represented in
00132         an internal floating point format. */
00133     int16_t dq[6];
00134     /*! Previous 2 samples of the quantized difference signal represented in an
00135         internal floating point format. */
00136     int16_t sr[2];
00137     /*! Delayed tone detect */
00138     int td;
00139     
00140     bitstream_state_t bs;
00141 
00142     g726_encoder_func_t enc_func;
00143     g726_decoder_func_t dec_func;
00144 } g726_state_t;
00145 
00146 #if defined(__cplusplus)
00147 extern "C"
00148 {
00149 #endif
00150 
00151 /*! Initialise a G.726 encode or decode context.
00152     \param s The G.726 context.
00153     \param bit_rate The required bit rate for the ADPCM data.
00154            The valid rates are 16000, 24000, 32000 and 40000.
00155     \param ext_coding The coding used outside G.726.
00156     \param packing One of the G.726_PACKING_xxx options.
00157     \return A pointer to the G.726 context, or NULL for error. */
00158 g726_state_t *g726_init(g726_state_t *s, int bit_rate, int ext_coding, int packing);
00159 
00160 /*! Free a G.726 encode or decode context.
00161     \param s The G.726 context.
00162     \return 0 for OK. */
00163 int g726_release(g726_state_t *s);
00164 
00165 /*! Decode a buffer of G.726 ADPCM data to linear PCM, a-law or u-law.
00166     \param s The G.726 context.
00167     \param amp The audio sample buffer.
00168     \param g726_data
00169     \param g726_bytes
00170     \return The number of samples returned. */
00171 int g726_decode(g726_state_t *s,
00172                 int16_t amp[],
00173                 const uint8_t g726_data[],
00174                 int g726_bytes);
00175 
00176 /*! Encode a buffer of linear PCM data to G.726 ADPCM.
00177     \param s The G.726 context.
00178     \param g726_data The G.726 data produced.
00179     \param amp The audio sample buffer.
00180     \param len The number of samples in the buffer.
00181     \return The number of bytes of G.726 data produced. */
00182 int g726_encode(g726_state_t *s,
00183                 uint8_t g726_data[],
00184                 const int16_t amp[],
00185                 int len);
00186 
00187 #if defined(__cplusplus)
00188 }
00189 #endif
00190 
00191 #endif
00192 /*- End of file ------------------------------------------------------------*/

Generated on Tue Jul 24 11:29:28 2007 for libspandsp by  doxygen 1.5.2