b0763ea9e37e43ab6a9f22b260fd958e68a93c0d
[Faustine.git] / interpretor / libsndfile-1.0.25 / src / ima_adpcm.c
1 /*
2 ** Copyright (C) 1999-2011 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 = calloc (1, pimasize)))
190 return SFE_MALLOC_FAILED ;
191
192 psf->codec_data = (void*) pima ;
193
194 pima->samples = pima->data ;
195 pima->block = (unsigned char*) (pima->data + samplesperblock * psf->sf.channels) ;
196
197 pima->channels = psf->sf.channels ;
198 pima->blocksize = blockalign ;
199 pima->samplesperblock = samplesperblock ;
200
201 psf->filelength = psf_get_filelen (psf) ;
202 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
203 psf->filelength - psf->dataoffset ;
204
205 if (pima->blocksize == 0)
206 { psf_log_printf (psf, "*** Error : pima->blocksize should not be zero.\n") ;
207 return SFE_INTERNAL ;
208 } ;
209
210 if (psf->datalength % pima->blocksize)
211 pima->blocks = psf->datalength / pima->blocksize + 1 ;
212 else
213 pima->blocks = psf->datalength / pima->blocksize ;
214
215 switch (SF_CONTAINER (psf->sf.format))
216 { case SF_FORMAT_WAV :
217 case SF_FORMAT_W64 :
218 count = 2 * (pima->blocksize - 4 * pima->channels) / pima->channels + 1 ;
219
220 if (pima->samplesperblock != count)
221 { psf_log_printf (psf, "*** Error : samplesperblock should be %d.\n", count) ;
222 return SFE_INTERNAL ;
223 } ;
224
225 pima->decode_block = wav_w64_ima_decode_block ;
226
227 psf->sf.frames = pima->samplesperblock * pima->blocks ;
228 break ;
229
230 case SF_FORMAT_AIFF :
231 psf_log_printf (psf, "still need to check block count\n") ;
232 pima->decode_block = aiff_ima_decode_block ;
233 psf->sf.frames = pima->samplesperblock * pima->blocks / pima->channels ;
234 break ;
235
236 default :
237 psf_log_printf (psf, "ima_reader_init: bad psf->sf.format\n") ;
238 return SFE_INTERNAL ;
239 } ;
240
241 pima->decode_block (psf, pima) ; /* Read first block. */
242
243 psf->read_short = ima_read_s ;
244 psf->read_int = ima_read_i ;
245 psf->read_float = ima_read_f ;
246 psf->read_double = ima_read_d ;
247
248 return 0 ;
249 } /* ima_reader_init */
250
251 static int
252 aiff_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
253 { unsigned char *blockdata ;
254 int chan, k, diff, bytecode, predictor ;
255 short step, stepindx, *sampledata ;
256
257 static int count = 0 ;
258 count ++ ;
259
260 pima->blockcount += pima->channels ;
261 pima->samplecount = 0 ;
262
263 if (pima->blockcount > pima->blocks)
264 { memset (pima->samples, 0, pima->samplesperblock * pima->channels * sizeof (short)) ;
265 return 1 ;
266 } ;
267
268 if ((k = psf_fread (pima->block, 1, pima->blocksize * pima->channels, psf)) != pima->blocksize * pima->channels)
269 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ;
270
271 /* Read and check the block header. */
272 for (chan = 0 ; chan < pima->channels ; chan++)
273 { blockdata = pima->block + chan * 34 ;
274 sampledata = pima->samples + chan ;
275
276 predictor = (blockdata [0] << 8) | (blockdata [1] & 0x80) ;
277
278 stepindx = blockdata [1] & 0x7F ;
279 stepindx = clamp_ima_step_index (stepindx) ;
280
281 /*
282 ** Pull apart the packed 4 bit samples and store them in their
283 ** correct sample positions.
284 */
285 for (k = 0 ; k < pima->blocksize - 2 ; k++)
286 { bytecode = blockdata [k + 2] ;
287 sampledata [pima->channels * (2 * k + 0)] = bytecode & 0xF ;
288 sampledata [pima->channels * (2 * k + 1)] = (bytecode >> 4) & 0xF ;
289 } ;
290
291 /* Decode the encoded 4 bit samples. */
292 for (k = 0 ; k < pima->samplesperblock ; k ++)
293 { step = ima_step_size [stepindx] ;
294
295 bytecode = pima->samples [pima->channels * k + chan] ;
296
297 stepindx += ima_indx_adjust [bytecode] ;
298 stepindx = clamp_ima_step_index (stepindx) ;
299
300 diff = step >> 3 ;
301 if (bytecode & 1) diff += step >> 2 ;
302 if (bytecode & 2) diff += step >> 1 ;
303 if (bytecode & 4) diff += step ;
304 if (bytecode & 8) diff = -diff ;
305
306 predictor += diff ;
307 if (predictor < -32768)
308 predictor = -32768 ;
309 else if (predictor > 32767)
310 predictor = 32767 ;
311
312 pima->samples [pima->channels * k + chan] = predictor ;
313 } ;
314 } ;
315
316 return 1 ;
317 } /* aiff_ima_decode_block */
318
319 static int
320 aiff_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
321 { int chan, k, step, diff, vpdiff, blockindx, indx ;
322 short bytecode, mask ;
323
324 /* Encode the block header. */
325 for (chan = 0 ; chan < pima->channels ; chan ++)
326 { blockindx = chan * pima->blocksize ;
327
328 pima->block [blockindx] = (pima->samples [chan] >> 8) & 0xFF ;
329 pima->block [blockindx + 1] = (pima->samples [chan] & 0x80) + (pima->stepindx [chan] & 0x7F) ;
330
331 pima->previous [chan] = pima->samples [chan] ;
332 } ;
333
334 /* Encode second and later samples for every block as a 4 bit value. */
335 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
336 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
337
338 diff = pima->samples [k] - pima->previous [chan] ;
339
340 bytecode = 0 ;
341 step = ima_step_size [pima->stepindx [chan]] ;
342 vpdiff = step >> 3 ;
343 if (diff < 0)
344 { bytecode = 8 ;
345 diff = -diff ;
346 } ;
347 mask = 4 ;
348 while (mask)
349 { if (diff >= step)
350 { bytecode |= mask ;
351 diff -= step ;
352 vpdiff += step ;
353 } ;
354 step >>= 1 ;
355 mask >>= 1 ;
356 } ;
357
358 if (bytecode & 8)
359 pima->previous [chan] -= vpdiff ;
360 else
361 pima->previous [chan] += vpdiff ;
362
363 if (pima->previous [chan] > 32767)
364 pima->previous [chan] = 32767 ;
365 else if (pima->previous [chan] < -32768)
366 pima->previous [chan] = -32768 ;
367
368 pima->stepindx [chan] += ima_indx_adjust [bytecode] ;
369
370 pima->stepindx [chan] = clamp_ima_step_index (pima->stepindx [chan]) ;
371 pima->samples [k] = bytecode ;
372 } ;
373
374 /* Pack the 4 bit encoded samples. */
375
376 for (chan = 0 ; chan < pima->channels ; chan ++)
377 { for (indx = pima->channels ; indx < pima->channels * pima->samplesperblock ; indx += 2 * pima->channels)
378 { blockindx = chan * pima->blocksize + 2 + indx / 2 ;
379
380 pima->block [blockindx] = pima->samples [indx] & 0x0F ;
381 pima->block [blockindx] |= (pima->samples [indx + chan] << 4) & 0xF0 ;
382 } ;
383 } ;
384
385 /* Write the block to disk. */
386
387 if ((k = psf_fwrite (pima->block, 1, pima->channels * pima->blocksize, psf)) != pima->channels * pima->blocksize)
388 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->channels * pima->blocksize) ;
389
390 memset (pima->samples, 0, pima->channels * pima->samplesperblock * sizeof (short)) ;
391 pima->samplecount = 0 ;
392 pima->blockcount ++ ;
393
394 return 1 ;
395 } /* aiff_ima_encode_block */
396
397 static int
398 wav_w64_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
399 { int chan, k, predictor, blockindx, indx, indxstart, diff ;
400 short step, bytecode, stepindx [2] ;
401
402 pima->blockcount ++ ;
403 pima->samplecount = 0 ;
404
405 if (pima->blockcount > pima->blocks)
406 { memset (pima->samples, 0, pima->samplesperblock * pima->channels * sizeof (short)) ;
407 return 1 ;
408 } ;
409
410 if ((k = psf_fread (pima->block, 1, pima->blocksize, psf)) != pima->blocksize)
411 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ;
412
413 /* Read and check the block header. */
414
415 for (chan = 0 ; chan < pima->channels ; chan++)
416 { predictor = pima->block [chan*4] | (pima->block [chan*4+1] << 8) ;
417 if (predictor & 0x8000)
418 predictor -= 0x10000 ;
419
420 stepindx [chan] = pima->block [chan*4+2] ;
421 stepindx [chan] = clamp_ima_step_index (stepindx [chan]) ;
422
423
424 if (pima->block [chan*4+3] != 0)
425 psf_log_printf (psf, "IMA ADPCM synchronisation error.\n") ;
426
427 pima->samples [chan] = predictor ;
428 } ;
429
430 /*
431 ** Pull apart the packed 4 bit samples and store them in their
432 ** correct sample positions.
433 */
434
435 blockindx = 4 * pima->channels ;
436
437 indxstart = pima->channels ;
438 while (blockindx < pima->blocksize)
439 { for (chan = 0 ; chan < pima->channels ; chan++)
440 { indx = indxstart + chan ;
441 for (k = 0 ; k < 4 ; k++)
442 { bytecode = pima->block [blockindx++] ;
443 pima->samples [indx] = bytecode & 0x0F ;
444 indx += pima->channels ;
445 pima->samples [indx] = (bytecode >> 4) & 0x0F ;
446 indx += pima->channels ;
447 } ;
448 } ;
449 indxstart += 8 * pima->channels ;
450 } ;
451
452 /* Decode the encoded 4 bit samples. */
453
454 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
455 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
456
457 bytecode = pima->samples [k] & 0xF ;
458
459 step = ima_step_size [stepindx [chan]] ;
460 predictor = pima->samples [k - pima->channels] ;
461
462 diff = step >> 3 ;
463 if (bytecode & 1)
464 diff += step >> 2 ;
465 if (bytecode & 2)
466 diff += step >> 1 ;
467 if (bytecode & 4)
468 diff += step ;
469 if (bytecode & 8)
470 diff = -diff ;
471
472 predictor += diff ;
473
474 if (predictor > 32767)
475 predictor = 32767 ;
476 else if (predictor < -32768)
477 predictor = -32768 ;
478
479 stepindx [chan] += ima_indx_adjust [bytecode] ;
480 stepindx [chan] = clamp_ima_step_index (stepindx [chan]) ;
481
482 pima->samples [k] = predictor ;
483 } ;
484
485 return 1 ;
486 } /* wav_w64_ima_decode_block */
487
488 static int
489 wav_w64_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
490 { int chan, k, step, diff, vpdiff, blockindx, indx, indxstart ;
491 short bytecode, mask ;
492
493 /* Encode the block header. */
494 for (chan = 0 ; chan < pima->channels ; chan++)
495 { pima->block [chan*4] = pima->samples [chan] & 0xFF ;
496 pima->block [chan*4+1] = (pima->samples [chan] >> 8) & 0xFF ;
497
498 pima->block [chan*4+2] = pima->stepindx [chan] ;
499 pima->block [chan*4+3] = 0 ;
500
501 pima->previous [chan] = pima->samples [chan] ;
502 } ;
503
504 /* Encode the samples as 4 bit. */
505
506 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
507 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
508
509 diff = pima->samples [k] - pima->previous [chan] ;
510
511 bytecode = 0 ;
512 step = ima_step_size [pima->stepindx [chan]] ;
513 vpdiff = step >> 3 ;
514 if (diff < 0)
515 { bytecode = 8 ;
516 diff = -diff ;
517 } ;
518 mask = 4 ;
519 while (mask)
520 { if (diff >= step)
521 { bytecode |= mask ;
522 diff -= step ;
523 vpdiff += step ;
524 } ;
525 step >>= 1 ;
526 mask >>= 1 ;
527 } ;
528
529 if (bytecode & 8)
530 pima->previous [chan] -= vpdiff ;
531 else
532 pima->previous [chan] += vpdiff ;
533
534 if (pima->previous [chan] > 32767)
535 pima->previous [chan] = 32767 ;
536 else if (pima->previous [chan] < -32768)
537 pima->previous [chan] = -32768 ;
538
539 pima->stepindx [chan] += ima_indx_adjust [bytecode] ;
540 pima->stepindx [chan] = clamp_ima_step_index (pima->stepindx [chan]) ;
541
542 pima->samples [k] = bytecode ;
543 } ;
544
545 /* Pack the 4 bit encoded samples. */
546
547 blockindx = 4 * pima->channels ;
548
549 indxstart = pima->channels ;
550 while (blockindx < pima->blocksize)
551 { for (chan = 0 ; chan < pima->channels ; chan++)
552 { indx = indxstart + chan ;
553 for (k = 0 ; k < 4 ; k++)
554 { pima->block [blockindx] = pima->samples [indx] & 0x0F ;
555 indx += pima->channels ;
556 pima->block [blockindx] |= (pima->samples [indx] << 4) & 0xF0 ;
557 indx += pima->channels ;
558 blockindx ++ ;
559 } ;
560 } ;
561 indxstart += 8 * pima->channels ;
562 } ;
563
564 /* Write the block to disk. */
565
566 if ((k = psf_fwrite (pima->block, 1, pima->blocksize, psf)) != pima->blocksize)
567 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->blocksize) ;
568
569 memset (pima->samples, 0, pima->samplesperblock * sizeof (short)) ;
570 pima->samplecount = 0 ;
571 pima->blockcount ++ ;
572
573 return 1 ;
574 } /* wav_w64_ima_encode_block */
575
576 static int
577 ima_read_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len)
578 { int count, total = 0, indx = 0 ;
579
580 while (indx < len)
581 { if (pima->blockcount >= pima->blocks && pima->samplecount >= pima->samplesperblock)
582 { memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ;
583 return total ;
584 } ;
585
586 if (pima->samplecount >= pima->samplesperblock)
587 pima->decode_block (psf, pima) ;
588
589 count = (pima->samplesperblock - pima->samplecount) * pima->channels ;
590 count = (len - indx > count) ? count : len - indx ;
591
592 memcpy (&(ptr [indx]), &(pima->samples [pima->samplecount * pima->channels]), count * sizeof (short)) ;
593 indx += count ;
594 pima->samplecount += count / pima->channels ;
595 total = indx ;
596 } ;
597
598 return total ;
599 } /* ima_read_block */
600
601 static sf_count_t
602 ima_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
603 { IMA_ADPCM_PRIVATE *pima ;
604 int readcount, count ;
605 sf_count_t total = 0 ;
606
607 if (! psf->codec_data)
608 return 0 ;
609 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
610
611 while (len > 0)
612 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
613
614 count = ima_read_block (psf, pima, ptr, readcount) ;
615
616 total += count ;
617 len -= count ;
618 if (count != readcount)
619 break ;
620 } ;
621
622 return total ;
623 } /* ima_read_s */
624
625 static sf_count_t
626 ima_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
627 { IMA_ADPCM_PRIVATE *pima ;
628 short *sptr ;
629 int k, bufferlen, readcount, count ;
630 sf_count_t total = 0 ;
631
632 if (! psf->codec_data)
633 return 0 ;
634 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
635
636 sptr = psf->u.sbuf ;
637 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
638 while (len > 0)
639 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
640 count = ima_read_block (psf, pima, sptr, readcount) ;
641 for (k = 0 ; k < readcount ; k++)
642 ptr [total + k] = ((int) sptr [k]) << 16 ;
643 total += count ;
644 len -= readcount ;
645 if (count != readcount)
646 break ;
647 } ;
648
649 return total ;
650 } /* ima_read_i */
651
652 static sf_count_t
653 ima_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
654 { IMA_ADPCM_PRIVATE *pima ;
655 short *sptr ;
656 int k, bufferlen, readcount, count ;
657 sf_count_t total = 0 ;
658 float normfact ;
659
660 if (! psf->codec_data)
661 return 0 ;
662 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
663
664 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
665
666 sptr = psf->u.sbuf ;
667 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
668 while (len > 0)
669 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
670 count = ima_read_block (psf, pima, sptr, readcount) ;
671 for (k = 0 ; k < readcount ; k++)
672 ptr [total + k] = normfact * (float) (sptr [k]) ;
673 total += count ;
674 len -= readcount ;
675 if (count != readcount)
676 break ;
677 } ;
678
679 return total ;
680 } /* ima_read_f */
681
682 static sf_count_t
683 ima_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
684 { IMA_ADPCM_PRIVATE *pima ;
685 short *sptr ;
686 int k, bufferlen, readcount, count ;
687 sf_count_t total = 0 ;
688 double normfact ;
689
690 if (! psf->codec_data)
691 return 0 ;
692 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
693
694 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
695
696 sptr = psf->u.sbuf ;
697 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
698 while (len > 0)
699 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
700 count = ima_read_block (psf, pima, sptr, readcount) ;
701 for (k = 0 ; k < readcount ; k++)
702 ptr [total + k] = normfact * (double) (sptr [k]) ;
703 total += count ;
704 len -= readcount ;
705 if (count != readcount)
706 break ;
707 } ;
708
709 return total ;
710 } /* ima_read_d */
711
712 static sf_count_t
713 ima_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
714 { IMA_ADPCM_PRIVATE *pima ;
715 int newblock, newsample ;
716
717 if (! psf->codec_data)
718 return 0 ;
719 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
720
721 if (psf->datalength < 0 || psf->dataoffset < 0)
722 { psf->error = SFE_BAD_SEEK ;
723 return PSF_SEEK_ERROR ;
724 } ;
725
726 if (offset == 0)
727 { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
728 pima->blockcount = 0 ;
729 pima->decode_block (psf, pima) ;
730 pima->samplecount = 0 ;
731 return 0 ;
732 } ;
733
734 if (offset < 0 || offset > pima->blocks * pima->samplesperblock)
735 { psf->error = SFE_BAD_SEEK ;
736 return PSF_SEEK_ERROR ;
737 } ;
738
739 newblock = offset / pima->samplesperblock ;
740 newsample = offset % pima->samplesperblock ;
741
742 if (mode == SFM_READ)
743 { psf_fseek (psf, psf->dataoffset + newblock * pima->blocksize, SEEK_SET) ;
744 pima->blockcount = newblock ;
745 pima->decode_block (psf, pima) ;
746 pima->samplecount = newsample ;
747 }
748 else
749 { /* What to do about write??? */
750 psf->error = SFE_BAD_SEEK ;
751 return PSF_SEEK_ERROR ;
752 } ;
753
754 return newblock * pima->samplesperblock + newsample ;
755 } /* ima_seek */
756
757 /*==========================================================================================
758 ** IMA ADPCM Write Functions.
759 */
760
761 static int
762 ima_writer_init (SF_PRIVATE *psf, int blockalign)
763 { IMA_ADPCM_PRIVATE *pima ;
764 int samplesperblock ;
765 unsigned int pimasize ;
766
767 if (psf->file.mode != SFM_WRITE)
768 return SFE_BAD_MODE_RW ;
769
770 samplesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
771
772 pimasize = sizeof (IMA_ADPCM_PRIVATE) + blockalign + 3 * psf->sf.channels * samplesperblock ;
773
774 if ((pima = calloc (1, pimasize)) == NULL)
775 return SFE_MALLOC_FAILED ;
776
777 psf->codec_data = (void*) pima ;
778
779 pima->channels = psf->sf.channels ;
780 pima->blocksize = blockalign ;
781 pima->samplesperblock = samplesperblock ;
782
783 pima->block = (unsigned char*) pima->data ;
784 pima->samples = (short*) (pima->data + blockalign) ;
785
786 pima->samplecount = 0 ;
787
788 switch (SF_CONTAINER (psf->sf.format))
789 { case SF_FORMAT_WAV :
790 case SF_FORMAT_W64 :
791 pima->encode_block = wav_w64_ima_encode_block ;
792 break ;
793
794 case SF_FORMAT_AIFF :
795 pima->encode_block = aiff_ima_encode_block ;
796 break ;
797
798 default :
799 psf_log_printf (psf, "ima_reader_init: bad psf->sf.format\n") ;
800 return SFE_INTERNAL ;
801 } ;
802
803 psf->write_short = ima_write_s ;
804 psf->write_int = ima_write_i ;
805 psf->write_float = ima_write_f ;
806 psf->write_double = ima_write_d ;
807
808 return 0 ;
809 } /* ima_writer_init */
810
811 /*==========================================================================================
812 */
813
814 static int
815 ima_write_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, const short *ptr, int len)
816 { int count, total = 0, indx = 0 ;
817
818 while (indx < len)
819 { count = (pima->samplesperblock - pima->samplecount) * pima->channels ;
820
821 if (count > len - indx)
822 count = len - indx ;
823
824 memcpy (&(pima->samples [pima->samplecount * pima->channels]), &(ptr [total]), count * sizeof (short)) ;
825 indx += count ;
826 pima->samplecount += count / pima->channels ;
827 total = indx ;
828
829 if (pima->samplecount >= pima->samplesperblock)
830 pima->encode_block (psf, pima) ;
831 } ;
832
833 return total ;
834 } /* ima_write_block */
835
836 static sf_count_t
837 ima_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
838 { IMA_ADPCM_PRIVATE *pima ;
839 int writecount, count ;
840 sf_count_t total = 0 ;
841
842 if (! psf->codec_data)
843 return 0 ;
844 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
845
846 while (len)
847 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
848
849 count = ima_write_block (psf, pima, ptr, writecount) ;
850
851 total += count ;
852 len -= count ;
853 if (count != writecount)
854 break ;
855 } ;
856
857 return total ;
858 } /* ima_write_s */
859
860 static sf_count_t
861 ima_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
862 { IMA_ADPCM_PRIVATE *pima ;
863 short *sptr ;
864 int k, bufferlen, writecount, count ;
865 sf_count_t total = 0 ;
866
867 if (! psf->codec_data)
868 return 0 ;
869 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
870
871 sptr = psf->u.sbuf ;
872 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
873 while (len > 0)
874 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
875 for (k = 0 ; k < writecount ; k++)
876 sptr [k] = ptr [total + k] >> 16 ;
877 count = ima_write_block (psf, pima, sptr, writecount) ;
878 total += count ;
879 len -= writecount ;
880 if (count != writecount)
881 break ;
882 } ;
883
884 return total ;
885 } /* ima_write_i */
886
887 static sf_count_t
888 ima_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
889 { IMA_ADPCM_PRIVATE *pima ;
890 short *sptr ;
891 int k, bufferlen, writecount, count ;
892 sf_count_t total = 0 ;
893 float normfact ;
894
895 if (! psf->codec_data)
896 return 0 ;
897 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
898
899 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
900
901 sptr = psf->u.sbuf ;
902 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
903 while (len > 0)
904 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
905 for (k = 0 ; k < writecount ; k++)
906 sptr [k] = lrintf (normfact * ptr [total + k]) ;
907 count = ima_write_block (psf, pima, sptr, writecount) ;
908 total += count ;
909 len -= writecount ;
910 if (count != writecount)
911 break ;
912 } ;
913
914 return total ;
915 } /* ima_write_f */
916
917 static sf_count_t
918 ima_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
919 { IMA_ADPCM_PRIVATE *pima ;
920 short *sptr ;
921 int k, bufferlen, writecount, count ;
922 sf_count_t total = 0 ;
923 double normfact ;
924
925 if (! psf->codec_data)
926 return 0 ;
927 pima = (IMA_ADPCM_PRIVATE*) psf->codec_data ;
928
929 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
930
931 sptr = psf->u.sbuf ;
932 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
933 while (len > 0)
934 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
935 for (k = 0 ; k < writecount ; k++)
936 sptr [k] = lrint (normfact * ptr [total + k]) ;
937 count = ima_write_block (psf, pima, sptr, writecount) ;
938 total += count ;
939 len -= writecount ;
940 if (count != writecount)
941 break ;
942 } ;
943
944 return total ;
945 } /* ima_write_d */
946