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, or 00014 * the Lesser GNU General Public License version 2.1, as published by 00015 * the Free Software Foundation. 00016 * 00017 * This program is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU General Public License for more details. 00021 * 00022 * You should have received a copy of the GNU General Public License 00023 * along with this program; if not, write to the Free Software 00024 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00025 * 00026 * $Id: g726.h,v 1.19 2008/02/09 15:32:26 steveu Exp $ 00027 */ 00028 00029 /*! \file */ 00030 00031 #if !defined(_SPANDSP_G726_H_) 00032 #define _SPANDSP_G726_H_ 00033 00034 /*! \page g726_page G.726 encoding and decoding 00035 \section g726_page_sec_1 What does it do? 00036 00037 The G.726 module is a bit exact implementation of the full ITU G.726 specification. 00038 It supports: 00039 - 16 kbps, 24kbps, 32kbps, and 40kbps operation. 00040 - Tandem adjustment, for interworking with A-law and u-law. 00041 - Annex A support, for use in environments not using A-law or u-law. 00042 00043 It passes the ITU tests. 00044 00045 \section g726_page_sec_2 How does it work? 00046 ???. 00047 */ 00048 00049 enum 00050 { 00051 G726_ENCODING_LINEAR = 0, /* Interworking with 16 bit signed linear */ 00052 G726_ENCODING_ULAW, /* Interworking with u-law */ 00053 G726_ENCODING_ALAW /* Interworking with A-law */ 00054 }; 00055 00056 enum 00057 { 00058 G726_PACKING_NONE = 0, 00059 G726_PACKING_LEFT = 1, 00060 G726_PACKING_RIGHT = 2 00061 }; 00062 00063 struct g726_state_s; 00064 00065 typedef int16_t (*g726_decoder_func_t)(struct g726_state_s *s, uint8_t code); 00066 00067 typedef uint8_t (*g726_encoder_func_t)(struct g726_state_s *s, int16_t amp); 00068 00069 /*! 00070 * The following is the definition of the state structure 00071 * used by the G.726 encoder and decoder to preserve their internal 00072 * state between successive calls. The meanings of the majority 00073 * of the state structure fields are explained in detail in the 00074 * CCITT Recommendation G.721. The field names are essentially indentical 00075 * to variable names in the bit level description of the coding algorithm 00076 * included in this Recommendation. 00077 */ 00078 typedef struct g726_state_s 00079 { 00080 /*! The bit rate */ 00081 int rate; 00082 /*! The external coding, for tandem operation */ 00083 int ext_coding; 00084 /*! The number of bits per sample */ 00085 unsigned int bits_per_sample; 00086 /*! One of the G.726_PACKING_xxx options */ 00087 int packing; 00088 00089 /*! Locked or steady state step size multiplier. */ 00090 int32_t yl; 00091 /*! Unlocked or non-steady state step size multiplier. */ 00092 int16_t yu; 00093 /*! int16_t term energy estimate. */ 00094 int16_t dms; 00095 /*! Long term energy estimate. */ 00096 int16_t dml; 00097 /*! Linear weighting coefficient of 'yl' and 'yu'. */ 00098 int16_t ap; 00099 00100 /*! Coefficients of pole portion of prediction filter. */ 00101 int16_t a[2]; 00102 /*! Coefficients of zero portion of prediction filter. */ 00103 int16_t b[6]; 00104 /*! Signs of previous two samples of a partially reconstructed signal. */ 00105 int16_t pk[2]; 00106 /*! Previous 6 samples of the quantized difference signal represented in 00107 an internal floating point format. */ 00108 int16_t dq[6]; 00109 /*! Previous 2 samples of the quantized difference signal represented in an 00110 internal floating point format. */ 00111 int16_t sr[2]; 00112 /*! Delayed tone detect */ 00113 int td; 00114 00115 /*! \brief The bit stream processing context. */ 00116 bitstream_state_t bs; 00117 00118 /*! \brief The current encoder function. */ 00119 g726_encoder_func_t enc_func; 00120 /*! \brief The current decoder function. */ 00121 g726_decoder_func_t dec_func; 00122 } g726_state_t; 00123 00124 #if defined(__cplusplus) 00125 extern "C" 00126 { 00127 #endif 00128 00129 /*! Initialise a G.726 encode or decode context. 00130 \param s The G.726 context. 00131 \param bit_rate The required bit rate for the ADPCM data. 00132 The valid rates are 16000, 24000, 32000 and 40000. 00133 \param ext_coding The coding used outside G.726. 00134 \param packing One of the G.726_PACKING_xxx options. 00135 \return A pointer to the G.726 context, or NULL for error. */ 00136 g726_state_t *g726_init(g726_state_t *s, int bit_rate, int ext_coding, int packing); 00137 00138 /*! Free a G.726 encode or decode context. 00139 \param s The G.726 context. 00140 \return 0 for OK. */ 00141 int g726_release(g726_state_t *s); 00142 00143 /*! Decode a buffer of G.726 ADPCM data to linear PCM, a-law or u-law. 00144 \param s The G.726 context. 00145 \param amp The audio sample buffer. 00146 \param g726_data 00147 \param g726_bytes 00148 \return The number of samples returned. */ 00149 int g726_decode(g726_state_t *s, 00150 int16_t amp[], 00151 const uint8_t g726_data[], 00152 int g726_bytes); 00153 00154 /*! Encode a buffer of linear PCM data to G.726 ADPCM. 00155 \param s The G.726 context. 00156 \param g726_data The G.726 data produced. 00157 \param amp The audio sample buffer. 00158 \param len The number of samples in the buffer. 00159 \return The number of bytes of G.726 data produced. */ 00160 int g726_encode(g726_state_t *s, 00161 uint8_t g726_data[], 00162 const int16_t amp[], 00163 int len); 00164 00165 #if defined(__cplusplus) 00166 } 00167 #endif 00168 00169 #endif 00170 /*- End of file ------------------------------------------------------------*/