Git init
[external/libsndfile.git] / src / ima_adpcm.c
1 /*
2 ** Copyright (C) 1999-2009 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
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.
8 **
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.
13 **
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.
17 */
18
19 #include        "sfconfig.h"
20
21 #include        <stdio.h>
22 #include        <stdlib.h>
23 #include        <string.h>
24 #include        <math.h>
25
26 #include        "sndfile.h"
27 #include        "sfendian.h"
28 #include        "common.h"
29
30 typedef struct IMA_ADPCM_PRIVATE_tag
31 {       int                     (*decode_block) (SF_PRIVATE *psf, struct IMA_ADPCM_PRIVATE_tag *pima) ;
32         int                     (*encode_block) (SF_PRIVATE *psf, struct IMA_ADPCM_PRIVATE_tag *pima) ;
33
34         int                             channels, blocksize, samplesperblock, blocks ;
35         int                             blockcount, samplecount ;
36         int                             previous [2] ;
37         int                             stepindx [2] ;
38         unsigned char   *block ;
39         short                   *samples ;
40 #if HAVE_FLEXIBLE_ARRAY
41         short                   data    [] ; /* ISO C99 struct flexible array. */
42 #else
43         short                   data    [0] ; /* This is a hack and might not work. */
44 #endif
45 } IMA_ADPCM_PRIVATE ;
46
47 /*============================================================================================
48 ** Predefined IMA ADPCM data.
49 */
50
51 static int ima_indx_adjust [16] =
52 {       -1, -1, -1, -1,         /* +0 - +3, decrease the step size */
53      2,  4,  6,  8,     /* +4 - +7, increase the step size */
54     -1, -1, -1, -1,             /* -0 - -3, decrease the step size */
55      2,  4,  6,  8,             /* -4 - -7, increase the step size */
56 } ;
57
58 static int ima_step_size [89] =
59 {       7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
60         50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230,
61         253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
62         1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
63         3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442,
64         11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
65         32767
66 } ;
67
68 static int ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
69 static int ima_writer_init (SF_PRIVATE *psf, int blockalign) ;
70
71 static int ima_read_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len) ;
72 static int ima_write_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, const short *ptr, int len) ;
73
74 static sf_count_t ima_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
75 static sf_count_t ima_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
76 static sf_count_t ima_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
77 static sf_count_t ima_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
78
79 static sf_count_t ima_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
80 static sf_count_t ima_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
81 static sf_count_t ima_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
82 static sf_count_t ima_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
83
84 static sf_count_t       ima_seek        (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
85
86 static int      ima_close       (SF_PRIVATE *psf) ;
87
88 static int wav_w64_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
89 static int wav_w64_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
90
91 /*-static int aiff_ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;-*/
92 static int aiff_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
93 static int aiff_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
94
95
96 static inline int
97 clamp_ima_step_index (int indx)
98 {       if (indx < 0)
99                 return 0 ;
100         if (indx >= ARRAY_LEN (ima_step_size))
101                 return ARRAY_LEN (ima_step_size) - 1 ;
102
103         return indx ;
104 } /* clamp_ima_step_index */
105
106 /*============================================================================================
107 ** IMA ADPCM Reader initialisation function.
108 */
109
110 int
111 wav_w64_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
112 {       int error ;
113
114         if (psf->codec_data != NULL)
115         {       psf_log_printf (psf, "*** psf->codec_data is not NULL.\n") ;
116                 return SFE_INTERNAL ;
117                 } ;
118
119         if (psf->file.mode == SFM_RDWR)
120                 return SFE_BAD_MODE_RW ;
121
122         if (psf->file.mode == SFM_READ)
123                 if ((error = ima_reader_init (psf, blockalign, samplesperblock)))
124                         return error ;
125
126         if (psf->file.mode == SFM_WRITE)
127                 if ((error = ima_writer_init (psf, blockalign)))
128                         return error ;
129
130         psf->codec_close = ima_close ;
131         psf->seek = ima_seek ;
132
133         return 0 ;
134 } /* wav_w64_ima_init */
135
136 int
137 aiff_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
138 {       int error ;
139
140         if (psf->file.mode == SFM_RDWR)
141                 return SFE_BAD_MODE_RW ;
142
143         if (psf->file.mode == SFM_READ)
144                 if ((error = ima_reader_init (psf, blockalign, samplesperblock)))
145                         return error ;
146
147         if (psf->file.mode == SFM_WRITE)
148                 if ((error = ima_writer_init (psf, blockalign)))
149                         return error ;
150
151         psf->codec_close = ima_close ;
152
153         return 0 ;
154 } /* aiff_ima_init */
155
156 static int
157 ima_close       (SF_PRIVATE *psf)
158 {       IMA_ADPCM_PRIVATE *pima ;
159
160         pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
161
162         if (psf->file.mode == SFM_WRITE)
163         {       /*      If a block has been partially assembled, write it out
164                 **      as the final block.
165                 */
166                 if (pima->samplecount && pima->samplecount < pima->samplesperblock)
167                         pima->encode_block (psf, pima) ;
168
169                 psf->sf.frames = pima->samplesperblock * pima->blockcount / psf->sf.channels ;
170                 } ;
171
172         return 0 ;
173 } /* ima_close */
174
175 /*============================================================================================
176 ** IMA ADPCM Read Functions.
177 */
178
179 static int
180 ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
181 {       IMA_ADPCM_PRIVATE       *pima ;
182         int             pimasize, count ;
183
184         if (psf->file.mode != SFM_READ)
185                 return SFE_BAD_MODE_RW ;
186
187         pimasize = sizeof (IMA_ADPCM_PRIVATE) + blockalign * psf->sf.channels + 3 * psf->sf.channels * samplesperblock ;
188
189         if (! (pima = malloc (pimasize)))
190                 return SFE_MALLOC_FAILED ;
191
192         psf->codec_data = (void*) pima ;
193
194         memset (pima, 0, pimasize) ;
195
196         pima->samples   = pima->data ;
197         pima->block             = (unsigned char*) (pima->data + samplesperblock * psf->sf.channels) ;
198
199         pima->channels                  = psf->sf.channels ;
200         pima->blocksize                 = blockalign ;
201         pima->samplesperblock   = samplesperblock ;
202
203         psf->filelength = psf_get_filelen (psf) ;
204         psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
205                                                         psf->filelength - psf->dataoffset ;
206
207     if (pima->blocksize == 0)
208         {       psf_log_printf (psf, "*** Error : pima->blocksize should not be zero.\n") ;
209                 return SFE_INTERNAL ;
210                 } ;
211
212         if (psf->datalength % pima->blocksize)
213                 pima->blocks = psf->datalength / pima->blocksize + 1 ;
214         else
215                 pima->blocks = psf->datalength / pima->blocksize ;
216
217         switch (SF_CONTAINER (psf->sf.format))
218         {       case SF_FORMAT_WAV :
219                 case SF_FORMAT_W64 :
220                                 count = 2 * (pima->blocksize - 4 * pima->channels) / pima->channels + 1 ;
221
222                                 if (pima->samplesperblock != count)
223                                 {       psf_log_printf (psf, "*** Error : samplesperblock should be %d.\n", count) ;
224                                         return SFE_INTERNAL ;
225                                         } ;
226
227                                 pima->decode_block = wav_w64_ima_decode_block ;
228
229                                 psf->sf.frames = pima->samplesperblock * pima->blocks ;
230                                 break ;
231
232                 case SF_FORMAT_AIFF :
233                                 psf_log_printf (psf, "still need to check block count\n") ;
234                                 pima->decode_block = aiff_ima_decode_block ;
235                                 psf->sf.frames = pima->samplesperblock * pima->blocks / pima->channels ;
236                                 break ;
237
238                 default :
239                                 psf_log_printf (psf, "ima_reader_init: bad psf->sf.format\n") ;
240                                 return SFE_INTERNAL ;
241                 } ;
242
243         pima->decode_block (psf, pima) ;        /* Read first block. */
244
245         psf->read_short         = ima_read_s ;
246         psf->read_int           = ima_read_i ;
247         psf->read_float         = ima_read_f ;
248         psf->read_double        = ima_read_d ;
249
250         return 0 ;
251 } /* ima_reader_init */
252
253 static int
254 aiff_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
255 {       unsigned char *blockdata ;
256         int             chan, k, diff, bytecode ;
257         short   step, stepindx, predictor, *sampledata ;
258
259 static int count = 0 ;
260 count ++ ;
261
262         pima->blockcount += pima->channels ;
263         pima->samplecount = 0 ;
264
265         if (pima->blockcount > pima->blocks)
266         {       memset (pima->samples, 0, pima->samplesperblock * pima->channels * sizeof (short)) ;
267                 return 1 ;
268                 } ;
269
270         if ((k = psf_fread (pima->block, 1, pima->blocksize * pima->channels, psf)) != pima->blocksize * pima->channels)
271                 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ;
272
273         /* Read and check the block header. */
274         for (chan = 0 ; chan < pima->channels ; chan++)
275         {       blockdata = pima->block + chan * 34 ;
276                 sampledata = pima->samples + chan ;
277
278                 predictor = (blockdata [0] << 8) | (blockdata [1] & 0x80) ;
279
280                 stepindx = blockdata [1] & 0x7F ;
281                 stepindx = clamp_ima_step_index (stepindx) ;
282
283                 /*
284                 **      Pull apart the packed 4 bit samples and store them in their
285                 **      correct sample positions.
286                 */
287                 for (k = 0 ; k < pima->blocksize - 2 ; k++)
288                 {       bytecode = blockdata [k + 2] ;
289                         sampledata [pima->channels * (2 * k + 0)] = bytecode & 0xF ;
290                         sampledata [pima->channels * (2 * k + 1)] = (bytecode >> 4) & 0xF ;
291                         } ;
292
293                 /* Decode the encoded 4 bit samples. */
294                 for (k = 0 ; k < pima->samplesperblock ; k ++)
295                 {       step = ima_step_size [stepindx] ;
296
297                         bytecode = pima->samples [pima->channels * k + chan] ;
298
299                         stepindx += ima_indx_adjust [bytecode] ;
300                         stepindx = clamp_ima_step_index (stepindx) ;
301
302                         diff = step >> 3 ;
303                         if (bytecode & 1)       diff += step >> 2 ;
304                         if (bytecode & 2)       diff += step >> 1 ;
305                         if (bytecode & 4)       diff += step ;
306                         if (bytecode & 8)       diff = -diff ;
307
308                         predictor += diff ;
309                         pima->samples [pima->channels * k + chan] = predictor ;
310                         } ;
311                 } ;
312
313         return 1 ;
314 } /* aiff_ima_decode_block */
315
316 static int
317 aiff_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
318 {       int             chan, k, step, diff, vpdiff, blockindx, indx ;
319         short   bytecode, mask ;
320
321         /* Encode the block header. */
322         for (chan = 0 ; chan < pima->channels ; chan ++)
323         {       blockindx = chan * pima->blocksize ;
324
325                 pima->block [blockindx] = (pima->samples [chan] >> 8) & 0xFF ;
326                 pima->block [blockindx + 1] = (pima->samples [chan] & 0x80) + (pima->stepindx [chan] & 0x7F) ;
327
328                 pima->previous [chan] = pima->samples [chan] ;
329                 } ;
330
331         /* Encode second and later samples for every block as a 4 bit value. */
332         for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
333         {       chan = (pima->channels > 1) ? (k % 2) : 0 ;
334
335                 diff = pima->samples [k] - pima->previous [chan] ;
336
337                 bytecode = 0 ;
338                 step = ima_step_size [pima->stepindx [chan]] ;
339                 vpdiff = step >> 3 ;
340                 if (diff < 0)
341                 {       bytecode = 8 ;
342                         diff = -diff ;
343                         } ;
344                 mask = 4 ;
345                 while (mask)
346                 {       if (diff >= step)
347                         {       bytecode |= mask ;
348                                 diff -= step ;
349                                 vpdiff += step ;
350                                 } ;
351                         step >>= 1 ;
352                         mask >>= 1 ;
353                         } ;
354
355                 if (bytecode & 8)
356                         pima->previous [chan] -= vpdiff ;
357                 else
358                         pima->previous [chan] += vpdiff ;
359
360                 if (pima->previous [chan] > 32767)
361                         pima->previous [chan] = 32767 ;
362                 else if (pima->previous [chan] < -32768)
363                         pima->previous [chan] = -32768 ;
364
365                 pima->stepindx [chan] += ima_indx_adjust [bytecode] ;
366
367                 pima->stepindx [chan] = clamp_ima_step_index (pima->stepindx [chan]) ;
368                 pima->samples [k] = bytecode ;
369                 } ;
370
371         /* Pack the 4 bit encoded samples. */
372
373         for (chan = 0 ; chan < pima->channels ; chan ++)
374         {       for (indx = pima->channels ; indx < pima->channels * pima->samplesperblock ; indx += 2 * pima->channels)
375                 {       blockindx = chan * pima->blocksize + 2 + indx / 2 ;
376
377                         pima->block [blockindx] = pima->samples [indx] & 0x0F ;
378                         pima->block [blockindx] |= (pima->samples [indx + chan] << 4) & 0xF0 ;
379                         } ;
380                 } ;
381
382         /* Write the block to disk. */
383
384         if ((k = psf_fwrite (pima->block, 1, pima->channels * pima->blocksize, psf)) != pima->channels * pima->blocksize)
385                 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->channels * pima->blocksize) ;
386
387         memset (pima->samples, 0, pima->channels * pima->samplesperblock * sizeof (short)) ;
388         pima->samplecount = 0 ;
389         pima->blockcount ++ ;
390
391         return 1 ;
392 } /* aiff_ima_encode_block */
393
394 static int
395 wav_w64_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
396 {       int             chan, k, current, blockindx, indx, indxstart, diff ;
397         short   step, bytecode, stepindx [2] ;
398
399         pima->blockcount ++ ;
400         pima->samplecount = 0 ;
401
402         if (pima->blockcount > pima->blocks)
403         {       memset (pima->samples, 0, pima->samplesperblock * pima->channels * sizeof (short)) ;
404                 return 1 ;
405                 } ;
406
407         if ((k = psf_fread (pima->block, 1, pima->blocksize, psf)) != pima->blocksize)
408                 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ;
409
410         /* Read and check the block header. */
411
412         for (chan = 0 ; chan < pima->channels ; chan++)
413         {       current = pima->block [chan*4] | (pima->block [chan*4+1] << 8) ;
414                 if (current & 0x8000)
415                         current -= 0x10000 ;
416
417                 stepindx [chan] = pima->block [chan*4+2] ;
418                 stepindx [chan] = clamp_ima_step_index (stepindx [chan]) ;
419
420
421                 if (pima->block [chan*4+3] != 0)
422                         psf_log_printf (psf, "IMA ADPCM synchronisation error.\n") ;
423
424                 pima->samples [chan] = current ;
425                 } ;
426
427         /*
428         **      Pull apart the packed 4 bit samples and store them in their
429         **      correct sample positions.
430         */
431
432         blockindx = 4 * pima->channels ;
433
434         indxstart = pima->channels ;
435         while (blockindx < pima->blocksize)
436         {       for (chan = 0 ; chan < pima->channels ; chan++)
437                 {       indx = indxstart + chan ;
438                         for (k = 0 ; k < 4 ; k++)
439                         {       bytecode = pima->block [blockindx++] ;
440                                 pima->samples [indx] = bytecode & 0x0F ;
441                                 indx += pima->channels ;
442                                 pima->samples [indx] = (bytecode >> 4) & 0x0F ;
443                                 indx += pima->channels ;
444                                 } ;
445                         } ;
446                 indxstart += 8 * pima->channels ;
447                 } ;
448
449         /* Decode the encoded 4 bit samples. */
450
451         for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
452         {       chan = (pima->channels > 1) ? (k % 2) : 0 ;
453
454                 bytecode = pima->samples [k] & 0xF ;
455
456                 step = ima_step_size [stepindx [chan]] ;
457                 current = pima->samples [k - pima->channels] ;
458
459                 diff = step >> 3 ;
460                 if (bytecode & 1)
461                         diff += step >> 2 ;
462                 if (bytecode & 2)
463                         diff += step >> 1 ;
464                 if (bytecode & 4)
465                         diff += step ;
466                 if (bytecode & 8)
467                         diff = -diff ;
468
469                 current += diff ;
470
471                 if (current > 32767)
472                         current = 32767 ;
473                 else if (current < -32768)
474                         current = -32768 ;
475
476                 stepindx [chan] += ima_indx_adjust [bytecode] ;
477                 stepindx [chan] = clamp_ima_step_index (stepindx [chan]) ;
478
479                 pima->samples [k] = current ;
480                 } ;
481
482         return 1 ;
483 } /* wav_w64_ima_decode_block */
484
485 static int
486 wav_w64_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
487 {       int             chan, k, step, diff, vpdiff, blockindx, indx, indxstart ;
488         short   bytecode, mask ;
489
490         /* Encode the block header. */
491         for (chan = 0 ; chan < pima->channels ; chan++)
492         {       pima->block [chan*4]    = pima->samples [chan] & 0xFF ;
493                 pima->block [chan*4+1]  = (pima->samples [chan] >> 8) & 0xFF ;
494
495                 pima->block [chan*4+2] = pima->stepindx [chan] ;
496                 pima->block [chan*4+3] = 0 ;
497
498                 pima->previous [chan] = pima->samples [chan] ;
499                 } ;
500
501         /* Encode the samples as 4 bit. */
502
503         for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
504         {       chan = (pima->channels > 1) ? (k % 2) : 0 ;
505
506                 diff = pima->samples [k] - pima->previous [chan] ;
507
508                 bytecode = 0 ;
509                 step = ima_step_size [pima->stepindx [chan]] ;
510                 vpdiff = step >> 3 ;
511                 if (diff < 0)
512                 {       bytecode = 8 ;
513                         diff = -diff ;
514                         } ;
515                 mask = 4 ;
516                 while (mask)
517                 {       if (diff >= step)
518                         {       bytecode |= mask ;
519                                 diff -= step ;
520                                 vpdiff += step ;
521                                 } ;
522                         step >>= 1 ;
523                         mask >>= 1 ;
524                         } ;
525
526                 if (bytecode & 8)
527                         pima->previous [chan] -= vpdiff ;
528                 else
529                         pima->previous [chan] += vpdiff ;
530
531                 if (pima->previous [chan] > 32767)
532                         pima->previous [chan] = 32767 ;
533                 else if (pima->previous [chan] < -32768)
534                         pima->previous [chan] = -32768 ;
535
536                 pima->stepindx [chan] += ima_indx_adjust [bytecode] ;
537                 pima->stepindx [chan] = clamp_ima_step_index (pima->stepindx [chan]) ;
538
539                 pima->samples [k] = bytecode ;
540                 } ;
541
542         /* Pack the 4 bit encoded samples. */
543
544         blockindx = 4 * pima->channels ;
545
546         indxstart = pima->channels ;
547         while (blockindx < pima->blocksize)
548         {       for (chan = 0 ; chan < pima->channels ; chan++)
549                 {       indx = indxstart + chan ;
550                         for (k = 0 ; k < 4 ; k++)
551                         {       pima->block [blockindx] = pima->samples [indx] & 0x0F ;
552                                 indx += pima->channels ;
553                                 pima->block [blockindx] |= (pima->samples [indx] << 4) & 0xF0 ;
554                                 indx += pima->channels ;
555                                 blockindx ++ ;
556                                 } ;
557                         } ;
558                 indxstart += 8 * pima->channels ;
559                 } ;
560
561         /* Write the block to disk. */
562
563         if ((k = psf_fwrite (pima->block, 1, pima->blocksize, psf)) != pima->blocksize)
564                 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->blocksize) ;
565
566         memset (pima->samples, 0, pima->samplesperblock * sizeof (short)) ;
567         pima->samplecount = 0 ;
568         pima->blockcount ++ ;
569
570         return 1 ;
571 } /* wav_w64_ima_encode_block */
572
573 static int
574 ima_read_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len)
575 {       int             count, total = 0, indx = 0 ;
576
577         while (indx < len)
578         {       if (pima->blockcount >= pima->blocks && pima->samplecount >= pima->samplesperblock)
579                 {       memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ;
580                         return total ;
581                         } ;
582
583                 if (pima->samplecount >= pima->samplesperblock)
584                         pima->decode_block (psf, pima) ;
585
586                 count = (pima->samplesperblock - pima->samplecount) * pima->channels ;
587                 count = (len - indx > count) ? count : len - indx ;
588
589                 memcpy (&(ptr [indx]), &(pima->samples [pima->samplecount * pima->channels]), count * sizeof (short)) ;
590                 indx += count ;
591                 pima->samplecount += count / pima->channels ;
592                 total = indx ;
593                 } ;
594
595         return total ;
596 } /* ima_read_block */
597
598 static sf_count_t
599 ima_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
600 {       IMA_ADPCM_PRIVATE       *pima ;
601         int                     readcount, count ;
602         sf_count_t      total = 0 ;
603
604         if (! psf->codec_data)
605                 return 0 ;
606         pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
607
608         while (len > 0)
609         {       readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
610
611                 count = ima_read_block (psf, pima, ptr, readcount) ;
612
613                 total += count ;
614                 len -= count ;
615                 if (count != readcount)
616                         break ;
617                 } ;
618
619         return total ;
620 } /* ima_read_s */
621
622 static sf_count_t
623 ima_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
624 {       IMA_ADPCM_PRIVATE *pima ;
625         short           *sptr ;
626         int                     k, bufferlen, readcount, count ;
627         sf_count_t      total = 0 ;
628
629         if (! psf->codec_data)
630                 return 0 ;
631         pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
632
633         sptr = psf->u.sbuf ;
634         bufferlen = ARRAY_LEN (psf->u.sbuf) ;
635         while (len > 0)
636         {       readcount = (len >= bufferlen) ? bufferlen : (int) len ;
637                 count = ima_read_block (psf, pima, sptr, readcount) ;
638                 for (k = 0 ; k < readcount ; k++)
639                         ptr [total + k] = ((int) sptr [k]) << 16 ;
640                 total += count ;
641                 len -= readcount ;
642                 if (count != readcount)
643                         break ;
644                 } ;
645
646         return total ;
647 } /* ima_read_i */
648
649 static sf_count_t
650 ima_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
651 {       IMA_ADPCM_PRIVATE *pima ;
652         short           *sptr ;
653         int                     k, bufferlen, readcount, count ;
654         sf_count_t      total = 0 ;
655         float           normfact ;
656
657         if (! psf->codec_data)
658                 return 0 ;
659         pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
660
661         normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
662
663         sptr = psf->u.sbuf ;
664         bufferlen = ARRAY_LEN (psf->u.sbuf) ;
665         while (len > 0)
666         {       readcount = (len >= bufferlen) ? bufferlen : (int) len ;
667                 count = ima_read_block (psf, pima, sptr, readcount) ;
668                 for (k = 0 ; k < readcount ; k++)
669                         ptr [total + k] = normfact * (float) (sptr [k]) ;
670                 total += count ;
671                 len -= readcount ;
672                 if (count != readcount)
673                         break ;
674                 } ;
675
676         return total ;
677 } /* ima_read_f */
678
679 static sf_count_t
680 ima_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
681 {       IMA_ADPCM_PRIVATE *pima ;
682         short           *sptr ;
683         int                     k, bufferlen, readcount, count ;
684         sf_count_t      total = 0 ;
685         double          normfact ;
686
687         if (! psf->codec_data)
688                 return 0 ;
689         pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
690
691         normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
692
693         sptr = psf->u.sbuf ;
694         bufferlen = ARRAY_LEN (psf->u.sbuf) ;
695         while (len > 0)
696         {       readcount = (len >= bufferlen) ? bufferlen : (int) len ;
697                 count = ima_read_block (psf, pima, sptr, readcount) ;
698                 for (k = 0 ; k < readcount ; k++)
699                         ptr [total + k] = normfact * (double) (sptr [k]) ;
700                 total += count ;
701                 len -= readcount ;
702                 if (count != readcount)
703                         break ;
704                 } ;
705
706         return total ;
707 } /* ima_read_d */
708
709 static sf_count_t
710 ima_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
711 {       IMA_ADPCM_PRIVATE *pima ;
712         int                     newblock, newsample ;
713
714         if (! psf->codec_data)
715                 return 0 ;
716         pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
717
718         if (psf->datalength < 0 || psf->dataoffset < 0)
719         {       psf->error = SFE_BAD_SEEK ;
720                 return PSF_SEEK_ERROR ;
721                 } ;
722
723         if (offset == 0)
724         {       psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
725                 pima->blockcount = 0 ;
726                 pima->decode_block (psf, pima) ;
727                 pima->samplecount = 0 ;
728                 return 0 ;
729                 } ;
730
731         if (offset < 0 || offset > pima->blocks * pima->samplesperblock)
732         {       psf->error = SFE_BAD_SEEK ;
733                 return  PSF_SEEK_ERROR ;
734                 } ;
735
736         newblock        = offset / pima->samplesperblock ;
737         newsample       = offset % pima->samplesperblock ;
738
739         if (mode == SFM_READ)
740         {       psf_fseek (psf, psf->dataoffset + newblock * pima->blocksize, SEEK_SET) ;
741                 pima->blockcount = newblock ;
742                 pima->decode_block (psf, pima) ;
743                 pima->samplecount = newsample ;
744                 }
745         else
746         {       /* What to do about write??? */
747                 psf->error = SFE_BAD_SEEK ;
748                 return  PSF_SEEK_ERROR ;
749                 } ;
750
751         return newblock * pima->samplesperblock + newsample ;
752 } /* ima_seek */
753
754 /*==========================================================================================
755 ** IMA ADPCM Write Functions.
756 */
757
758 static int
759 ima_writer_init (SF_PRIVATE *psf, int blockalign)
760 {       IMA_ADPCM_PRIVATE       *pima ;
761         int                                     samplesperblock ;
762         unsigned int            pimasize ;
763
764         if (psf->file.mode != SFM_WRITE)
765                 return SFE_BAD_MODE_RW ;
766
767         samplesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
768
769         pimasize = sizeof (IMA_ADPCM_PRIVATE) + blockalign + 3 * psf->sf.channels * samplesperblock ;
770
771         if ((pima = calloc (1, pimasize)) == NULL)
772                 return SFE_MALLOC_FAILED ;
773
774         psf->codec_data = (void*) pima ;
775
776         pima->channels                  = psf->sf.channels ;
777         pima->blocksize                 = blockalign ;
778         pima->samplesperblock   = samplesperblock ;
779
780         pima->block             = (unsigned char*) pima->data ;
781         pima->samples   = (short*) (pima->data + blockalign) ;
782
783         pima->samplecount = 0 ;
784
785         switch (SF_CONTAINER (psf->sf.format))
786         {       case SF_FORMAT_WAV :
787                 case SF_FORMAT_W64 :
788                                 pima->encode_block = wav_w64_ima_encode_block ;
789                                 break ;
790
791                 case SF_FORMAT_AIFF :
792                                 pima->encode_block = aiff_ima_encode_block ;
793                                 break ;
794
795                 default :
796                                 psf_log_printf (psf, "ima_reader_init: bad psf->sf.format\n") ;
797                                 return SFE_INTERNAL ;
798                 } ;
799
800         psf->write_short        = ima_write_s ;
801         psf->write_int          = ima_write_i ;
802         psf->write_float        = ima_write_f ;
803         psf->write_double       = ima_write_d ;
804
805         return 0 ;
806 } /* ima_writer_init */
807
808 /*==========================================================================================
809 */
810
811 static int
812 ima_write_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, const short *ptr, int len)
813 {       int             count, total = 0, indx = 0 ;
814
815         while (indx < len)
816         {       count = (pima->samplesperblock - pima->samplecount) * pima->channels ;
817
818                 if (count > len - indx)
819                         count = len - indx ;
820
821                 memcpy (&(pima->samples [pima->samplecount * pima->channels]), &(ptr [total]), count * sizeof (short)) ;
822                 indx += count ;
823                 pima->samplecount += count / pima->channels ;
824                 total = indx ;
825
826                 if (pima->samplecount >= pima->samplesperblock)
827                         pima->encode_block (psf, pima) ;
828                 } ;
829
830         return total ;
831 } /* ima_write_block */
832
833 static sf_count_t
834 ima_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
835 {       IMA_ADPCM_PRIVATE       *pima ;
836         int                     writecount, count ;
837         sf_count_t      total = 0 ;
838
839         if (! psf->codec_data)
840                 return 0 ;
841         pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
842
843         while (len)
844         {       writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
845
846                 count = ima_write_block (psf, pima, ptr, writecount) ;
847
848                 total += count ;
849                 len -= count ;
850                 if (count != writecount)
851                         break ;
852                 } ;
853
854         return total ;
855 } /* ima_write_s */
856
857 static sf_count_t
858 ima_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
859 {       IMA_ADPCM_PRIVATE *pima ;
860         short           *sptr ;
861         int                     k, bufferlen, writecount, count ;
862         sf_count_t      total = 0 ;
863
864         if (! psf->codec_data)
865                 return 0 ;
866         pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
867
868         sptr = psf->u.sbuf ;
869         bufferlen = ARRAY_LEN (psf->u.sbuf) ;
870         while (len > 0)
871         {       writecount = (len >= bufferlen) ? bufferlen : (int) len ;
872                 for (k = 0 ; k < writecount ; k++)
873                         sptr [k] = ptr [total + k] >> 16 ;
874                 count = ima_write_block (psf, pima, sptr, writecount) ;
875                 total += count ;
876                 len -= writecount ;
877                 if (count != writecount)
878                         break ;
879                 } ;
880
881         return total ;
882 } /* ima_write_i */
883
884 static sf_count_t
885 ima_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
886 {       IMA_ADPCM_PRIVATE *pima ;
887         short           *sptr ;
888         int                     k, bufferlen, writecount, count ;
889         sf_count_t      total = 0 ;
890         float           normfact ;
891
892         if (! psf->codec_data)
893                 return 0 ;
894         pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
895
896         normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
897
898         sptr = psf->u.sbuf ;
899         bufferlen = ARRAY_LEN (psf->u.sbuf) ;
900         while (len > 0)
901         {       writecount = (len >= bufferlen) ? bufferlen : (int) len ;
902                 for (k = 0 ; k < writecount ; k++)
903                         sptr [k] = lrintf (normfact * ptr [total + k]) ;
904                 count = ima_write_block (psf, pima, sptr, writecount) ;
905                 total += count ;
906                 len -= writecount ;
907                 if (count != writecount)
908                         break ;
909                 } ;
910
911         return total ;
912 } /* ima_write_f */
913
914 static sf_count_t
915 ima_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
916 {       IMA_ADPCM_PRIVATE *pima ;
917         short           *sptr ;
918         int                     k, bufferlen, writecount, count ;
919         sf_count_t      total = 0 ;
920         double          normfact ;
921
922         if (! psf->codec_data)
923                 return 0 ;
924         pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
925
926         normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
927
928         sptr = psf->u.sbuf ;
929         bufferlen = ARRAY_LEN (psf->u.sbuf) ;
930         while (len > 0)
931         {       writecount = (len >= bufferlen) ? bufferlen : (int) len ;
932                 for (k = 0 ; k < writecount ; k++)
933                         sptr [k] = lrint (normfact * ptr [total + k]) ;
934                 count = ima_write_block (psf, pima, sptr, writecount) ;
935                 total += count ;
936                 len -= writecount ;
937                 if (count != writecount)
938                         break ;
939                 } ;
940
941         return total ;
942 } /* ima_write_d */
943