2 ** Copyright (C) 2002-2005 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 of the License, or
7 ** (at your option) any later version.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU Lesser General Public License for more details.
14 ** You should have received a copy of the GNU Lesser General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 ** This is the OKI / Dialogic ADPCM encoder/decoder. It converts from
21 ** 12 bit linear sample data to a 4 bit ADPCM.
23 ** Implemented from the description found here:
25 ** http://www.comptek.ru:8100/telephony/tnotes/tt1-13.html
27 ** and compared against the encoder/decoder found here:
29 ** http://ibiblio.org/pub/linux/apps/sound/convert/vox.tar.gz
40 #include "float_cast.h"
43 #define VOX_DATA_LEN 2048
44 #define PCM_DATA_LEN (VOX_DATA_LEN *2)
50 int vox_bytes, pcm_samples ;
52 unsigned char vox_data [VOX_DATA_LEN] ;
53 short pcm_data [PCM_DATA_LEN] ;
56 static int vox_adpcm_encode_block (VOX_ADPCM_PRIVATE *pvox) ;
57 static int vox_adpcm_decode_block (VOX_ADPCM_PRIVATE *pvox) ;
59 static short vox_adpcm_decode (char code, VOX_ADPCM_PRIVATE *pvox) ;
60 static char vox_adpcm_encode (short samp, VOX_ADPCM_PRIVATE *pvox) ;
62 static sf_count_t vox_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
63 static sf_count_t vox_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
64 static sf_count_t vox_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
65 static sf_count_t vox_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
67 static sf_count_t vox_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
68 static sf_count_t vox_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
69 static sf_count_t vox_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
70 static sf_count_t vox_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
72 static int vox_read_block (SF_PRIVATE *psf, VOX_ADPCM_PRIVATE *pvox, short *ptr, int len) ;
74 /*============================================================================================
75 ** Predefined OKI ADPCM encoder/decoder tables.
78 static short step_size_table [49] =
79 { 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60,
80 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209,
81 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
82 724, 796, 876, 963, 1060, 1166, 1282, 1408, 1552
83 } ; /* step_size_table */
85 static short step_adjust_table [8] =
86 { -1, -1, -1, -1, 2, 4, 6, 8
87 } ; /* step_adjust_table */
89 /*------------------------------------------------------------------------------
93 vox_adpcm_init (SF_PRIVATE *psf)
94 { VOX_ADPCM_PRIVATE *pvox = NULL ;
96 if (psf->mode == SFM_RDWR)
97 return SFE_BAD_MODE_RW ;
99 if (psf->mode == SFM_WRITE && psf->sf.channels != 1)
100 return SFE_CHANNEL_COUNT ;
102 if ((pvox = malloc (sizeof (VOX_ADPCM_PRIVATE))) == NULL)
103 return SFE_MALLOC_FAILED ;
105 psf->fdata = (void*) pvox ;
106 memset (pvox, 0, sizeof (VOX_ADPCM_PRIVATE)) ;
108 if (psf->mode == SFM_WRITE)
109 { psf->write_short = vox_write_s ;
110 psf->write_int = vox_write_i ;
111 psf->write_float = vox_write_f ;
112 psf->write_double = vox_write_d ;
115 { psf_log_printf (psf, "Header-less OKI Dialogic ADPCM encoded file.\n") ;
116 psf_log_printf (psf, "Setting up for 8kHz, mono, Vox ADPCM.\n") ;
118 psf->read_short = vox_read_s ;
119 psf->read_int = vox_read_i ;
120 psf->read_float = vox_read_f ;
121 psf->read_double = vox_read_d ;
124 /* Standard sample rate chennels etc. */
125 if (psf->sf.samplerate < 1)
126 psf->sf.samplerate = 8000 ;
127 psf->sf.channels = 1 ;
129 psf->sf.frames = psf->filelength * 2 ;
131 psf->sf.seekable = SF_FALSE ;
133 /* Seek back to start of data. */
134 if (psf_fseek (psf, 0 , SEEK_SET) == -1)
135 return SFE_BAD_SEEK ;
138 } /* vox_adpcm_init */
140 /*------------------------------------------------------------------------------
144 vox_adpcm_encode (short samp, VOX_ADPCM_PRIVATE *pvox)
146 short diff, error, stepsize ;
148 stepsize = step_size_table [pvox->step_index] ;
151 diff = samp - pvox->last ;
159 if (error >= stepsize)
160 { code = code | 0x04 ;
164 if (error >= stepsize / 2)
165 { code = code | 0x02 ;
166 error -= stepsize / 2 ;
169 if (error >= stepsize / 4)
173 ** To close the feedback loop, the deocder is used to set the
174 ** estimate of last sample and in doing so, also set the step_index.
176 pvox->last = vox_adpcm_decode (code, pvox) ;
179 } /* vox_adpcm_encode */
182 vox_adpcm_decode (char code, VOX_ADPCM_PRIVATE *pvox)
183 { short diff, error, stepsize, samp ;
185 stepsize = step_size_table [pvox->step_index] ;
187 error = stepsize / 8 ;
190 error += stepsize / 4 ;
193 error += stepsize / 2 ;
198 diff = (code & 0x08) ? -error : error ;
199 samp = pvox->last + diff ;
210 pvox->step_index += step_adjust_table [code & 0x7] ;
212 if (pvox->step_index < 0)
213 pvox->step_index = 0 ;
214 if (pvox->step_index > 48)
215 pvox->step_index = 48 ;
218 } /* vox_adpcm_decode */
221 vox_adpcm_encode_block (VOX_ADPCM_PRIVATE *pvox)
222 { unsigned char code ;
225 /* If data_count is odd, add an extra zero valued sample. */
226 if (pvox->pcm_samples & 1)
227 pvox->pcm_data [pvox->pcm_samples++] = 0 ;
229 for (j = k = 0 ; k < pvox->pcm_samples ; j++)
230 { code = vox_adpcm_encode (pvox->pcm_data [k++] / 16, pvox) << 4 ;
231 code |= vox_adpcm_encode (pvox->pcm_data [k++] / 16, pvox) ;
232 pvox->vox_data [j] = code ;
235 pvox->vox_bytes = j ;
238 } /* vox_adpcm_encode_block */
241 vox_adpcm_decode_block (VOX_ADPCM_PRIVATE *pvox)
242 { unsigned char code ;
245 for (j = k = 0 ; j < pvox->vox_bytes ; j++)
246 { code = pvox->vox_data [j] ;
247 pvox->pcm_data [k++] = 16 * vox_adpcm_decode ((code >> 4) & 0x0f, pvox) ;
248 pvox->pcm_data [k++] = 16 * vox_adpcm_decode (code & 0x0f, pvox) ;
251 pvox->pcm_samples = k ;
254 } /* vox_adpcm_decode_block */
256 /*==============================================================================
260 vox_read_block (SF_PRIVATE *psf, VOX_ADPCM_PRIVATE *pvox, short *ptr, int len)
264 { pvox->vox_bytes = (len - indx > PCM_DATA_LEN) ? VOX_DATA_LEN : (len - indx + 1) / 2 ;
266 if ((k = psf_fread (pvox->vox_data, 1, pvox->vox_bytes, psf)) != pvox->vox_bytes)
267 { if (psf_ftell (psf) + k != psf->filelength)
268 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pvox->vox_bytes) ;
273 pvox->vox_bytes = k ;
275 vox_adpcm_decode_block (pvox) ;
277 memcpy (&(ptr [indx]), pvox->pcm_data, pvox->pcm_samples * sizeof (short)) ;
278 indx += pvox->pcm_samples ;
282 } /* vox_read_block */
286 vox_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
287 { VOX_ADPCM_PRIVATE *pvox ;
288 int readcount, count ;
289 sf_count_t total = 0 ;
293 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
296 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
298 count = vox_read_block (psf, pvox, ptr, readcount) ;
302 if (count != readcount)
310 vox_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
311 { VOX_ADPCM_PRIVATE *pvox ;
313 int k, bufferlen, readcount, count ;
314 sf_count_t total = 0 ;
318 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
321 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
323 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
324 count = vox_read_block (psf, pvox, sptr, readcount) ;
325 for (k = 0 ; k < readcount ; k++)
326 ptr [total + k] = ((int) sptr [k]) << 16 ;
329 if (count != readcount)
337 vox_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
338 { VOX_ADPCM_PRIVATE *pvox ;
340 int k, bufferlen, readcount, count ;
341 sf_count_t total = 0 ;
346 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
348 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
351 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
353 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
354 count = vox_read_block (psf, pvox, sptr, readcount) ;
355 for (k = 0 ; k < readcount ; k++)
356 ptr [total + k] = normfact * (float) (sptr [k]) ;
359 if (count != readcount)
367 vox_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
368 { VOX_ADPCM_PRIVATE *pvox ;
370 int k, bufferlen, readcount, count ;
371 sf_count_t total = 0 ;
376 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
378 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
381 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
383 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
384 count = vox_read_block (psf, pvox, sptr, readcount) ;
385 for (k = 0 ; k < readcount ; k++)
386 ptr [total + k] = normfact * (double) (sptr [k]) ;
389 if (count != readcount)
396 /*------------------------------------------------------------------------------
400 vox_write_block (SF_PRIVATE *psf, VOX_ADPCM_PRIVATE *pvox, const short *ptr, int len)
404 { pvox->pcm_samples = (len - indx > PCM_DATA_LEN) ? PCM_DATA_LEN : len - indx ;
406 memcpy (pvox->pcm_data, &(ptr [indx]), pvox->pcm_samples * sizeof (short)) ;
408 vox_adpcm_encode_block (pvox) ;
410 if ((k = psf_fwrite (pvox->vox_data, 1, pvox->vox_bytes, psf)) != pvox->vox_bytes)
411 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pvox->vox_bytes) ;
413 indx += pvox->pcm_samples ;
417 } /* vox_write_block */
420 vox_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
421 { VOX_ADPCM_PRIVATE *pvox ;
422 int writecount, count ;
423 sf_count_t total = 0 ;
427 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
430 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
432 count = vox_write_block (psf, pvox, ptr, writecount) ;
436 if (count != writecount)
444 vox_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
445 { VOX_ADPCM_PRIVATE *pvox ;
447 int k, bufferlen, writecount, count ;
448 sf_count_t total = 0 ;
452 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
455 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
457 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
458 for (k = 0 ; k < writecount ; k++)
459 sptr [k] = ptr [total + k] >> 16 ;
460 count = vox_write_block (psf, pvox, sptr, writecount) ;
463 if (count != writecount)
471 vox_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
472 { VOX_ADPCM_PRIVATE *pvox ;
474 int k, bufferlen, writecount, count ;
475 sf_count_t total = 0 ;
480 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
482 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
485 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
487 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
488 for (k = 0 ; k < writecount ; k++)
489 sptr [k] = lrintf (normfact * ptr [total + k]) ;
490 count = vox_write_block (psf, pvox, sptr, writecount) ;
493 if (count != writecount)
501 vox_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
502 { VOX_ADPCM_PRIVATE *pvox ;
504 int k, bufferlen, writecount, count ;
505 sf_count_t total = 0 ;
510 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
512 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
515 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
517 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
518 for (k = 0 ; k < writecount ; k++)
519 sptr [k] = lrint (normfact * ptr [total + k]) ;
520 count = vox_write_block (psf, pvox, sptr, writecount) ;
523 if (count != writecount)
532 ** Do not edit or modify anything in this comment block.
533 ** The arch-tag line is a file identity tag for the GNU Arch
534 ** revision control system.
536 ** arch-tag: e15e97fe-ff9d-4b46-a489-7059fb2d0b1e