2 ** Copyright (C) 1999-2011 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.
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
) ;
34 int channels
, blocksize
, samplesperblock
, blocks
;
35 int blockcount
, samplecount
;
38 unsigned char *block
;
40 #if HAVE_FLEXIBLE_ARRAY
41 short data
[] ; /* ISO C99 struct flexible array. */
43 short data
[0] ; /* This is a hack and might not work. */
47 /*============================================================================================
48 ** Predefined IMA ADPCM data.
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 */
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,
68 static int ima_reader_init (SF_PRIVATE
*psf
, int blockalign
, int samplesperblock
) ;
69 static int ima_writer_init (SF_PRIVATE
*psf
, int blockalign
) ;
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
) ;
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
) ;
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
) ;
84 static sf_count_t
ima_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
) ;
86 static int ima_close (SF_PRIVATE
*psf
) ;
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
) ;
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
) ;
97 clamp_ima_step_index (int indx
)
100 if (indx
>= ARRAY_LEN (ima_step_size
))
101 return ARRAY_LEN (ima_step_size
) - 1 ;
104 } /* clamp_ima_step_index */
106 /*============================================================================================
107 ** IMA ADPCM Reader initialisation function.
111 wav_w64_ima_init (SF_PRIVATE
*psf
, int blockalign
, int samplesperblock
)
114 if (psf
->codec_data
!= NULL
)
115 { psf_log_printf (psf
, "*** psf->codec_data is not NULL.\n") ;
116 return SFE_INTERNAL
;
119 if (psf
->file
.mode
== SFM_RDWR
)
120 return SFE_BAD_MODE_RW
;
122 if (psf
->file
.mode
== SFM_READ
)
123 if ((error
= ima_reader_init (psf
, blockalign
, samplesperblock
)))
126 if (psf
->file
.mode
== SFM_WRITE
)
127 if ((error
= ima_writer_init (psf
, blockalign
)))
130 psf
->codec_close
= ima_close
;
131 psf
->seek
= ima_seek
;
134 } /* wav_w64_ima_init */
137 aiff_ima_init (SF_PRIVATE
*psf
, int blockalign
, int samplesperblock
)
140 if (psf
->file
.mode
== SFM_RDWR
)
141 return SFE_BAD_MODE_RW
;
143 if (psf
->file
.mode
== SFM_READ
)
144 if ((error
= ima_reader_init (psf
, blockalign
, samplesperblock
)))
147 if (psf
->file
.mode
== SFM_WRITE
)
148 if ((error
= ima_writer_init (psf
, blockalign
)))
151 psf
->codec_close
= ima_close
;
154 } /* aiff_ima_init */
157 ima_close (SF_PRIVATE
*psf
)
158 { IMA_ADPCM_PRIVATE
*pima
;
160 pima
= (IMA_ADPCM_PRIVATE
*) psf
->codec_data
;
162 if (psf
->file
.mode
== SFM_WRITE
)
163 { /* If a block has been partially assembled, write it out
164 ** as the final block.
166 if (pima
->samplecount
&& pima
->samplecount
< pima
->samplesperblock
)
167 pima
->encode_block (psf
, pima
) ;
169 psf
->sf
.frames
= pima
->samplesperblock
* pima
->blockcount
/ psf
->sf
.channels
;
175 /*============================================================================================
176 ** IMA ADPCM Read Functions.
180 ima_reader_init (SF_PRIVATE
*psf
, int blockalign
, int samplesperblock
)
181 { IMA_ADPCM_PRIVATE
*pima
;
182 int pimasize
, count
;
184 if (psf
->file
.mode
!= SFM_READ
)
185 return SFE_BAD_MODE_RW
;
187 pimasize
= sizeof (IMA_ADPCM_PRIVATE
) + blockalign
* psf
->sf
.channels
+ 3 * psf
->sf
.channels
* samplesperblock
;
189 if (! (pima
= calloc (1, pimasize
)))
190 return SFE_MALLOC_FAILED
;
192 psf
->codec_data
= (void*) pima
;
194 pima
->samples
= pima
->data
;
195 pima
->block
= (unsigned char*) (pima
->data
+ samplesperblock
* psf
->sf
.channels
) ;
197 pima
->channels
= psf
->sf
.channels
;
198 pima
->blocksize
= blockalign
;
199 pima
->samplesperblock
= samplesperblock
;
201 psf
->filelength
= psf_get_filelen (psf
) ;
202 psf
->datalength
= (psf
->dataend
) ? psf
->dataend
- psf
->dataoffset
:
203 psf
->filelength
- psf
->dataoffset
;
205 if (pima
->blocksize
== 0)
206 { psf_log_printf (psf
, "*** Error : pima->blocksize should not be zero.\n") ;
207 return SFE_INTERNAL
;
210 if (psf
->datalength
% pima
->blocksize
)
211 pima
->blocks
= psf
->datalength
/ pima
->blocksize
+ 1 ;
213 pima
->blocks
= psf
->datalength
/ pima
->blocksize
;
215 switch (SF_CONTAINER (psf
->sf
.format
))
216 { case SF_FORMAT_WAV
:
218 count
= 2 * (pima
->blocksize
- 4 * pima
->channels
) / pima
->channels
+ 1 ;
220 if (pima
->samplesperblock
!= count
)
221 { psf_log_printf (psf
, "*** Error : samplesperblock should be %d.\n", count
) ;
222 return SFE_INTERNAL
;
225 pima
->decode_block
= wav_w64_ima_decode_block
;
227 psf
->sf
.frames
= pima
->samplesperblock
* pima
->blocks
;
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
;
237 psf_log_printf (psf
, "ima_reader_init: bad psf->sf.format\n") ;
238 return SFE_INTERNAL
;
241 pima
->decode_block (psf
, pima
) ; /* Read first block. */
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
;
249 } /* ima_reader_init */
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
;
257 static int count
= 0 ;
260 pima
->blockcount
+= pima
->channels
;
261 pima
->samplecount
= 0 ;
263 if (pima
->blockcount
> pima
->blocks
)
264 { memset (pima
->samples
, 0, pima
->samplesperblock
* pima
->channels
* sizeof (short)) ;
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
) ;
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
;
276 predictor
= (blockdata
[0] << 8) | (blockdata
[1] & 0x80) ;
278 stepindx
= blockdata
[1] & 0x7F ;
279 stepindx
= clamp_ima_step_index (stepindx
) ;
282 ** Pull apart the packed 4 bit samples and store them in their
283 ** correct sample positions.
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 ;
291 /* Decode the encoded 4 bit samples. */
292 for (k
= 0 ; k
< pima
->samplesperblock
; k
++)
293 { step
= ima_step_size
[stepindx
] ;
295 bytecode
= pima
->samples
[pima
->channels
* k
+ chan
] ;
297 stepindx
+= ima_indx_adjust
[bytecode
] ;
298 stepindx
= clamp_ima_step_index (stepindx
) ;
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
;
307 if (predictor
< -32768)
309 else if (predictor
> 32767)
312 pima
->samples
[pima
->channels
* k
+ chan
] = predictor
;
317 } /* aiff_ima_decode_block */
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
;
324 /* Encode the block header. */
325 for (chan
= 0 ; chan
< pima
->channels
; chan
++)
326 { blockindx
= chan
* pima
->blocksize
;
328 pima
->block
[blockindx
] = (pima
->samples
[chan
] >> 8) & 0xFF ;
329 pima
->block
[blockindx
+ 1] = (pima
->samples
[chan
] & 0x80) + (pima
->stepindx
[chan
] & 0x7F) ;
331 pima
->previous
[chan
] = pima
->samples
[chan
] ;
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 ;
338 diff
= pima
->samples
[k
] - pima
->previous
[chan
] ;
341 step
= ima_step_size
[pima
->stepindx
[chan
]] ;
359 pima
->previous
[chan
] -= vpdiff
;
361 pima
->previous
[chan
] += vpdiff
;
363 if (pima
->previous
[chan
] > 32767)
364 pima
->previous
[chan
] = 32767 ;
365 else if (pima
->previous
[chan
] < -32768)
366 pima
->previous
[chan
] = -32768 ;
368 pima
->stepindx
[chan
] += ima_indx_adjust
[bytecode
] ;
370 pima
->stepindx
[chan
] = clamp_ima_step_index (pima
->stepindx
[chan
]) ;
371 pima
->samples
[k
] = bytecode
;
374 /* Pack the 4 bit encoded samples. */
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 ;
380 pima
->block
[blockindx
] = pima
->samples
[indx
] & 0x0F ;
381 pima
->block
[blockindx
] |= (pima
->samples
[indx
+ chan
] << 4) & 0xF0 ;
385 /* Write the block to disk. */
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
) ;
390 memset (pima
->samples
, 0, pima
->channels
* pima
->samplesperblock
* sizeof (short)) ;
391 pima
->samplecount
= 0 ;
392 pima
->blockcount
++ ;
395 } /* aiff_ima_encode_block */
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] ;
402 pima
->blockcount
++ ;
403 pima
->samplecount
= 0 ;
405 if (pima
->blockcount
> pima
->blocks
)
406 { memset (pima
->samples
, 0, pima
->samplesperblock
* pima
->channels
* sizeof (short)) ;
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
) ;
413 /* Read and check the block header. */
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 ;
420 stepindx
[chan
] = pima
->block
[chan
*4+2] ;
421 stepindx
[chan
] = clamp_ima_step_index (stepindx
[chan
]) ;
424 if (pima
->block
[chan
*4+3] != 0)
425 psf_log_printf (psf
, "IMA ADPCM synchronisation error.\n") ;
427 pima
->samples
[chan
] = predictor
;
431 ** Pull apart the packed 4 bit samples and store them in their
432 ** correct sample positions.
435 blockindx
= 4 * pima
->channels
;
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
;
449 indxstart
+= 8 * pima
->channels
;
452 /* Decode the encoded 4 bit samples. */
454 for (k
= pima
->channels
; k
< (pima
->samplesperblock
* pima
->channels
) ; k
++)
455 { chan
= (pima
->channels
> 1) ? (k
% 2) : 0 ;
457 bytecode
= pima
->samples
[k
] & 0xF ;
459 step
= ima_step_size
[stepindx
[chan
]] ;
460 predictor
= pima
->samples
[k
- pima
->channels
] ;
474 if (predictor
> 32767)
476 else if (predictor
< -32768)
479 stepindx
[chan
] += ima_indx_adjust
[bytecode
] ;
480 stepindx
[chan
] = clamp_ima_step_index (stepindx
[chan
]) ;
482 pima
->samples
[k
] = predictor
;
486 } /* wav_w64_ima_decode_block */
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
;
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 ;
498 pima
->block
[chan
*4+2] = pima
->stepindx
[chan
] ;
499 pima
->block
[chan
*4+3] = 0 ;
501 pima
->previous
[chan
] = pima
->samples
[chan
] ;
504 /* Encode the samples as 4 bit. */
506 for (k
= pima
->channels
; k
< (pima
->samplesperblock
* pima
->channels
) ; k
++)
507 { chan
= (pima
->channels
> 1) ? (k
% 2) : 0 ;
509 diff
= pima
->samples
[k
] - pima
->previous
[chan
] ;
512 step
= ima_step_size
[pima
->stepindx
[chan
]] ;
530 pima
->previous
[chan
] -= vpdiff
;
532 pima
->previous
[chan
] += vpdiff
;
534 if (pima
->previous
[chan
] > 32767)
535 pima
->previous
[chan
] = 32767 ;
536 else if (pima
->previous
[chan
] < -32768)
537 pima
->previous
[chan
] = -32768 ;
539 pima
->stepindx
[chan
] += ima_indx_adjust
[bytecode
] ;
540 pima
->stepindx
[chan
] = clamp_ima_step_index (pima
->stepindx
[chan
]) ;
542 pima
->samples
[k
] = bytecode
;
545 /* Pack the 4 bit encoded samples. */
547 blockindx
= 4 * pima
->channels
;
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
;
561 indxstart
+= 8 * pima
->channels
;
564 /* Write the block to disk. */
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
) ;
569 memset (pima
->samples
, 0, pima
->samplesperblock
* sizeof (short)) ;
570 pima
->samplecount
= 0 ;
571 pima
->blockcount
++ ;
574 } /* wav_w64_ima_encode_block */
577 ima_read_block (SF_PRIVATE
*psf
, IMA_ADPCM_PRIVATE
*pima
, short *ptr
, int len
)
578 { int count
, total
= 0, indx
= 0 ;
581 { if (pima
->blockcount
>= pima
->blocks
&& pima
->samplecount
>= pima
->samplesperblock
)
582 { memset (&(ptr
[indx
]), 0, (size_t) ((len
- indx
) * sizeof (short))) ;
586 if (pima
->samplecount
>= pima
->samplesperblock
)
587 pima
->decode_block (psf
, pima
) ;
589 count
= (pima
->samplesperblock
- pima
->samplecount
) * pima
->channels
;
590 count
= (len
- indx
> count
) ? count
: len
- indx
;
592 memcpy (&(ptr
[indx
]), &(pima
->samples
[pima
->samplecount
* pima
->channels
]), count
* sizeof (short)) ;
594 pima
->samplecount
+= count
/ pima
->channels
;
599 } /* ima_read_block */
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 ;
607 if (! psf
->codec_data
)
609 pima
= (IMA_ADPCM_PRIVATE
*) psf
->codec_data
;
612 { readcount
= (len
> 0x10000000) ? 0x10000000 : (int) len
;
614 count
= ima_read_block (psf
, pima
, ptr
, readcount
) ;
618 if (count
!= readcount
)
626 ima_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
627 { IMA_ADPCM_PRIVATE
*pima
;
629 int k
, bufferlen
, readcount
, count
;
630 sf_count_t total
= 0 ;
632 if (! psf
->codec_data
)
634 pima
= (IMA_ADPCM_PRIVATE
*) psf
->codec_data
;
637 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
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 ;
645 if (count
!= readcount
)
653 ima_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
654 { IMA_ADPCM_PRIVATE
*pima
;
656 int k
, bufferlen
, readcount
, count
;
657 sf_count_t total
= 0 ;
660 if (! psf
->codec_data
)
662 pima
= (IMA_ADPCM_PRIVATE
*) psf
->codec_data
;
664 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x8000) : 1.0 ;
667 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
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
]) ;
675 if (count
!= readcount
)
683 ima_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
684 { IMA_ADPCM_PRIVATE
*pima
;
686 int k
, bufferlen
, readcount
, count
;
687 sf_count_t total
= 0 ;
690 if (! psf
->codec_data
)
692 pima
= (IMA_ADPCM_PRIVATE
*) psf
->codec_data
;
694 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x8000) : 1.0 ;
697 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
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
]) ;
705 if (count
!= readcount
)
713 ima_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
)
714 { IMA_ADPCM_PRIVATE
*pima
;
715 int newblock
, newsample
;
717 if (! psf
->codec_data
)
719 pima
= (IMA_ADPCM_PRIVATE
*) psf
->codec_data
;
721 if (psf
->datalength
< 0 || psf
->dataoffset
< 0)
722 { psf
->error
= SFE_BAD_SEEK
;
723 return PSF_SEEK_ERROR
;
727 { psf_fseek (psf
, psf
->dataoffset
, SEEK_SET
) ;
728 pima
->blockcount
= 0 ;
729 pima
->decode_block (psf
, pima
) ;
730 pima
->samplecount
= 0 ;
734 if (offset
< 0 || offset
> pima
->blocks
* pima
->samplesperblock
)
735 { psf
->error
= SFE_BAD_SEEK
;
736 return PSF_SEEK_ERROR
;
739 newblock
= offset
/ pima
->samplesperblock
;
740 newsample
= offset
% pima
->samplesperblock
;
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
;
749 { /* What to do about write??? */
750 psf
->error
= SFE_BAD_SEEK
;
751 return PSF_SEEK_ERROR
;
754 return newblock
* pima
->samplesperblock
+ newsample
;
757 /*==========================================================================================
758 ** IMA ADPCM Write Functions.
762 ima_writer_init (SF_PRIVATE
*psf
, int blockalign
)
763 { IMA_ADPCM_PRIVATE
*pima
;
764 int samplesperblock
;
765 unsigned int pimasize
;
767 if (psf
->file
.mode
!= SFM_WRITE
)
768 return SFE_BAD_MODE_RW
;
770 samplesperblock
= 2 * (blockalign
- 4 * psf
->sf
.channels
) / psf
->sf
.channels
+ 1 ;
772 pimasize
= sizeof (IMA_ADPCM_PRIVATE
) + blockalign
+ 3 * psf
->sf
.channels
* samplesperblock
;
774 if ((pima
= calloc (1, pimasize
)) == NULL
)
775 return SFE_MALLOC_FAILED
;
777 psf
->codec_data
= (void*) pima
;
779 pima
->channels
= psf
->sf
.channels
;
780 pima
->blocksize
= blockalign
;
781 pima
->samplesperblock
= samplesperblock
;
783 pima
->block
= (unsigned char*) pima
->data
;
784 pima
->samples
= (short*) (pima
->data
+ blockalign
) ;
786 pima
->samplecount
= 0 ;
788 switch (SF_CONTAINER (psf
->sf
.format
))
789 { case SF_FORMAT_WAV
:
791 pima
->encode_block
= wav_w64_ima_encode_block
;
794 case SF_FORMAT_AIFF
:
795 pima
->encode_block
= aiff_ima_encode_block
;
799 psf_log_printf (psf
, "ima_reader_init: bad psf->sf.format\n") ;
800 return SFE_INTERNAL
;
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
;
809 } /* ima_writer_init */
811 /*==========================================================================================
815 ima_write_block (SF_PRIVATE
*psf
, IMA_ADPCM_PRIVATE
*pima
, const short *ptr
, int len
)
816 { int count
, total
= 0, indx
= 0 ;
819 { count
= (pima
->samplesperblock
- pima
->samplecount
) * pima
->channels
;
821 if (count
> len
- indx
)
824 memcpy (&(pima
->samples
[pima
->samplecount
* pima
->channels
]), &(ptr
[total
]), count
* sizeof (short)) ;
826 pima
->samplecount
+= count
/ pima
->channels
;
829 if (pima
->samplecount
>= pima
->samplesperblock
)
830 pima
->encode_block (psf
, pima
) ;
834 } /* ima_write_block */
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 ;
842 if (! psf
->codec_data
)
844 pima
= (IMA_ADPCM_PRIVATE
*) psf
->codec_data
;
847 { writecount
= (len
> 0x10000000) ? 0x10000000 : (int) len
;
849 count
= ima_write_block (psf
, pima
, ptr
, writecount
) ;
853 if (count
!= writecount
)
861 ima_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
862 { IMA_ADPCM_PRIVATE
*pima
;
864 int k
, bufferlen
, writecount
, count
;
865 sf_count_t total
= 0 ;
867 if (! psf
->codec_data
)
869 pima
= (IMA_ADPCM_PRIVATE
*) psf
->codec_data
;
872 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
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
) ;
880 if (count
!= writecount
)
888 ima_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
889 { IMA_ADPCM_PRIVATE
*pima
;
891 int k
, bufferlen
, writecount
, count
;
892 sf_count_t total
= 0 ;
895 if (! psf
->codec_data
)
897 pima
= (IMA_ADPCM_PRIVATE
*) psf
->codec_data
;
899 normfact
= (psf
->norm_float
== SF_TRUE
) ? (1.0 * 0x7FFF) : 1.0 ;
902 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
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
) ;
910 if (count
!= writecount
)
918 ima_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
919 { IMA_ADPCM_PRIVATE
*pima
;
921 int k
, bufferlen
, writecount
, count
;
922 sf_count_t total
= 0 ;
925 if (! psf
->codec_data
)
927 pima
= (IMA_ADPCM_PRIVATE
*) psf
->codec_data
;
929 normfact
= (psf
->norm_double
== SF_TRUE
) ? (1.0 * 0x7FFF) : 1.0 ;
932 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
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
) ;
940 if (count
!= writecount
)