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