00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * imaadpcm.c - Conversion routines between linear 16 bit PCM data and 00005 * IMA/DVI/Intel ADPCM format. 00006 * 00007 * Written by Steve Underwood <steveu@coppice.org> 00008 * 00009 * Copyright (C) 2004 Steve Underwood 00010 * 00011 * Based on a bit from here, a bit from there, eye of toad, 00012 * ear of bat, etc - plus, of course, my own 2 cents. 00013 * 00014 * All rights reserved. 00015 * 00016 * This program is free software; you can redistribute it and/or modify 00017 * it under the terms of the GNU General Public License version 2, or 00018 * the Lesser GNU General Public License version 2.1, as published by 00019 * the Free Software Foundation. 00020 * 00021 * This program is distributed in the hope that it will be useful, 00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00024 * GNU General Public License for more details. 00025 * 00026 * You should have received a copy of the GNU General Public License 00027 * along with this program; if not, write to the Free Software 00028 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00029 * 00030 * $Id: ima_adpcm.h,v 1.18 2008/02/09 15:33:40 steveu Exp $ 00031 */ 00032 00033 /*! \file */ 00034 00035 #if !defined(_SPANDSP_IMA_ADPCM_H_) 00036 #define _SPANDSP_IMA_ADPCM_H_ 00037 00038 /*! \page ima_adpcm_page IMA/DVI/Intel ADPCM encoding and decoding 00039 \section ima_adpcm_page_sec_1 What does it do? 00040 IMA ADPCM offers a good balance of simplicity and quality at a rate of 00041 32kbps. 00042 00043 \section ima_adpcm_page_sec_2 How does it work? 00044 00045 \section ima_adpcm_page_sec_3 How do I use it? 00046 */ 00047 00048 enum 00049 { 00050 /*! IMA4 is the original IMA ADPCM variant */ 00051 IMA_ADPCM_IMA4 = 0, 00052 /*! DVI4 is the IMA ADPCM variant defined in RFC3551 */ 00053 IMA_ADPCM_DVI4 = 1, 00054 /*! VDVI is the variable bit rate IMA ADPCM variant defined in RFC3551 */ 00055 IMA_ADPCM_VDVI = 2 00056 }; 00057 00058 /*! 00059 IMA (DVI/Intel) ADPCM conversion state descriptor. This defines the state of 00060 a single working instance of the IMA ADPCM converter. This is used for 00061 either linear to ADPCM or ADPCM to linear conversion. 00062 */ 00063 typedef struct 00064 { 00065 int variant; 00066 /*! \brief The size of a chunk, in samples. */ 00067 int chunk_size; 00068 /*! \brief The last state of the ADPCM algorithm. */ 00069 int last; 00070 /*! \brief Current index into the step size table. */ 00071 int step_index; 00072 /*! \brief The current IMA code byte in progress. */ 00073 uint16_t ima_byte; 00074 int bits; 00075 } ima_adpcm_state_t; 00076 00077 #if defined(__cplusplus) 00078 extern "C" 00079 { 00080 #endif 00081 00082 /*! Initialise an IMA ADPCM encode or decode context. 00083 \param s The IMA ADPCM context 00084 \param variant ??? 00085 \param chunk_size The size of a chunk, in samples. A chunk size of 00086 zero sample samples means treat each encode or decode operation 00087 as a chunk. 00088 \return A pointer to the IMA ADPCM context, or NULL for error. */ 00089 ima_adpcm_state_t *ima_adpcm_init(ima_adpcm_state_t *s, int variant, int chunk_size); 00090 00091 /*! Free an IMA ADPCM encode or decode context. 00092 \param s The IMA ADPCM context. 00093 \return 0 for OK. */ 00094 int ima_adpcm_release(ima_adpcm_state_t *s); 00095 00096 /*! Encode a buffer of linear PCM data to IMA ADPCM. 00097 \param s The IMA ADPCM context. 00098 \param ima_data The IMA ADPCM data produced. 00099 \param amp The audio sample buffer. 00100 \param len The number of samples in the buffer. 00101 \return The number of bytes of IMA ADPCM data produced. */ 00102 int ima_adpcm_encode(ima_adpcm_state_t *s, 00103 uint8_t ima_data[], 00104 const int16_t amp[], 00105 int len); 00106 00107 /*! Decode a buffer of IMA ADPCM data to linear PCM. 00108 \param s The IMA ADPCM context. 00109 \param amp The audio sample buffer. 00110 \param ima_data 00111 \param ima_bytes 00112 \return The number of samples returned. */ 00113 int ima_adpcm_decode(ima_adpcm_state_t *s, 00114 int16_t amp[], 00115 const uint8_t ima_data[], 00116 int ima_bytes); 00117 00118 #if defined(__cplusplus) 00119 } 00120 #endif 00121 00122 #endif 00123 /*- End of file ------------------------------------------------------------*/