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