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