g722_1/g722_1.h

00001 /*
00002  * g722_1 - a library for the G.722.1 and Annex C codecs
00003  *
00004  * g722_1.h
00005  *
00006  * Adapted by Steve Underwood <steveu@coppice.org> from the reference
00007  * code supplied with ITU G.722.1, which is:
00008  *
00009  *   © 2004 Polycom, Inc.
00010  *   All rights reserved.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00015  *
00016  * $Id: g722_1.h,v 1.14 2008/10/17 13:18:21 steveu Exp $
00017  */
00018 
00019 #if !defined(_G722_1_G722_1_H_)
00020 #define _G722_1_G722_1_H_
00021 
00022 typedef enum
00023 {
00024     /*! \brief Basic G.722.1 sampling rate */
00025     G722_1_SAMPLE_RATE_16000 = 16000,
00026     /*! \brief G.722.1 Annex C sampling rate */
00027     G722_1_SAMPLE_RATE_32000 = 32000
00028 } g722_1_sample_rates_t;
00029 
00030 typedef enum
00031 {
00032     /*! \brief Bit rate usable at either sampling rate. */
00033     G722_1_BIT_RATE_24000 = 24000,
00034     /*! \brief Bit rate usable at either sampling rate. */
00035     G722_1_BIT_RATE_32000 = 32000,
00036     /*! \brief Bit rate usable at 32000 samples per second. */
00037     G722_1_BIT_RATE_48000 = 48000
00038 } g722_1_bit_rates_t;
00039 
00040 #define MAX_SAMPLE_RATE         32000
00041 /* Frames are 20ms */
00042 #define MAX_FRAME_SIZE          (MAX_SAMPLE_RATE/50)
00043 #define MAX_DCT_LENGTH          640
00044 
00045 /* Max bit rate is 48000 bits/sec. */
00046 #define MAX_BITS_PER_FRAME      960
00047 
00048 #define NUMBER_OF_REGIONS       14
00049 #define MAX_NUMBER_OF_REGIONS   28
00050 
00051 /*! Bitstream handler state */
00052 typedef struct
00053 {
00054     /*! The bit stream. */
00055     uint32_t bitstream;
00056     /*! The residual bits in bitstream. */
00057     int residue;
00058 } g722_1_bitstream_state_t;
00059 
00060 typedef struct
00061 {
00062     int16_t code_bit_count;      /* bit count of the current word */
00063     int16_t current_word;        /* current word in the bitstream being processed */
00064     uint16_t *code_word_ptr;     /* pointer to the bitstream */
00065 } g722_1_bitstream_t;
00066 
00067 typedef struct
00068 {
00069     int16_t seed0;
00070     int16_t seed1;
00071     int16_t seed2;
00072     int16_t seed3;
00073 } g722_1_rand_t;
00074 
00075 typedef struct
00076 {
00077     int bit_rate;
00078     int sample_rate;
00079     int frame_size;
00080     int number_of_regions;
00081     int number_of_bits_per_frame;
00082     int bytes_per_frame;
00083     int number_of_16bit_words_per_frame;
00084 #if defined(G722_1_USE_FIXED_POINT)
00085     int16_t history[MAX_FRAME_SIZE];
00086 #else
00087     float history[MAX_FRAME_SIZE];
00088     float scale_factor;
00089 #endif
00090     g722_1_bitstream_state_t bitstream;
00091 } g722_1_encode_state_t;
00092 
00093 typedef struct
00094 {
00095     int bit_rate;
00096     int sample_rate;
00097     int frame_size;
00098     int number_of_regions;
00099     int number_of_bits_per_frame;
00100     int bytes_per_frame;
00101     int number_of_16bit_words_per_frame;
00102     int16_t words;
00103     int16_t old_mag_shift;
00104 #if defined(G722_1_USE_FIXED_POINT)
00105     int16_t old_decoder_mlt_coefs[MAX_DCT_LENGTH];
00106     int16_t old_samples[MAX_DCT_LENGTH >> 1];
00107 #else
00108     float old_decoder_mlt_coefs[MAX_DCT_LENGTH];
00109     float old_samples[MAX_DCT_LENGTH >> 1];
00110 #endif
00111     g722_1_bitstream_t bitobj;
00112     g722_1_bitstream_state_t bitstream;
00113     const uint8_t *code_ptr;
00114     int16_t number_of_bits_left;
00115     g722_1_rand_t randobj;
00116 } g722_1_decode_state_t;
00117 
00118 #if defined(__cplusplus)
00119 extern "C"
00120 {
00121 #endif
00122 
00123 /*! Initialise a G.722.1 encode context.
00124     \param s The G.722.1 encode context.
00125     \param bit_rate The required bit rate for the G.722.1 data.
00126            The valid rates are 48000, 32000 and 24000.
00127     \param sample_rate The required sampling rate.
00128            The valid rates are 16000 and 32000.
00129     \return A pointer to the G.722.1 encode context, or NULL for error. */
00130 g722_1_encode_state_t *g722_1_encode_init(g722_1_encode_state_t *s, int bit_rate, int sample_rate);
00131 
00132 /*! Release a G.722.1 encode context.
00133     \param s The G.722.1 encode context.
00134     \return 0. */
00135 int g722_1_encode_release(g722_1_encode_state_t *s);
00136 
00137 /*! Encode a buffer of linear PCM data to G.722.1
00138     \param s The G.722.1 encode context.
00139     \param g722_1_data The G.722.1 data produced.
00140     \param amp The audio sample buffer.
00141     \param len The number of samples in the buffer.
00142     \return The number of bytes of G.722.1 data produced. */
00143 int g722_1_encode(g722_1_encode_state_t *s, uint8_t g722_1_data[], const int16_t amp[], int len);
00144 
00145 /*! Change the bit rate for an G.722.1 decode context.
00146     \param s The G.722.1 decode context.
00147     \param bit_rate The required bit rate for the G.722.1 data.
00148            The valid rates are 48000, 32000 and 24000.
00149     \return 0 for OK, or -1 for a bad parameter. */
00150 int g722_1_encode_set_rate(g722_1_encode_state_t *s, int bit_rate);
00151 
00152 /*! Initialise a G.722.1 decode context.
00153     \param s The G.722.1 decode context.
00154     \param bit_rate The required bit rate for the G.722.1 data.
00155            The valid rates are 48000, 32000 and 24000.
00156     \param sample_rate The required sampling rate.
00157            The valid rates are 16000 and 32000.
00158     \return A pointer to the G.722.1 decode context, or NULL for error. */
00159 g722_1_decode_state_t *g722_1_decode_init(g722_1_decode_state_t *s, int bit_rate, int sample_rate);
00160 
00161 /*! Release a G.722.1 decode context.
00162     \param s The G.722.1 decode context.
00163     \return 0. */
00164 int g722_1_decode_release(g722_1_decode_state_t *s);
00165 
00166 /*! Decode a buffer of G.722.1 data to linear PCM.
00167     \param s The G.722.1 decode context.
00168     \param amp The audio sample buffer.
00169     \param g722_1_data
00170     \param len
00171     \return The number of samples returned. */
00172 int g722_1_decode(g722_1_decode_state_t *s, int16_t amp[], const uint8_t g722_1_data[], int len);
00173 
00174 /*! Produce linear PCM data to fill in where received G.722.1 data is missing.
00175     \param s The G.722.1 decode context.
00176     \param amp The audio sample buffer.
00177     \param g722_1_data
00178     \param len
00179     \return The number of samples returned. */
00180 int g722_1_fillin(g722_1_decode_state_t *s, int16_t amp[], const uint8_t g722_1_data[], int len);
00181 
00182 /*! Change the bit rate for an G.722.1 decode context.
00183     \param s The G.722.1 decode context.
00184     \param bit_rate The required bit rate for the G.722.1 data.
00185            The valid rates are 48000, 32000 and 24000.
00186     \return 0 for OK, or -1 for a bad parameter. */
00187 int g722_1_decode_set_rate(g722_1_decode_state_t *s, int bit_rate);
00188 
00189 #if defined(__cplusplus)
00190 }
00191 #endif
00192 
00193 #endif
00194 /*- End of file ------------------------------------------------------------*/

Generated on 16 Jul 2011 for libg722_1 by  doxygen 1.6.1