7d04f85b9528ece12e24021525b62f4711f2061b
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / src / ms_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 #include "wav_w64.h"
30
31 /* These required here because we write the header in this file. */
32
33 #define RIFF_MARKER (MAKE_MARKER ('R', 'I', 'F', 'F'))
34 #define WAVE_MARKER (MAKE_MARKER ('W', 'A', 'V', 'E'))
35 #define fmt_MARKER (MAKE_MARKER ('f', 'm', 't', ' '))
36 #define fact_MARKER (MAKE_MARKER ('f', 'a', 'c', 't'))
37 #define data_MARKER (MAKE_MARKER ('d', 'a', 't', 'a'))
38
39 #define WAVE_FORMAT_MS_ADPCM 0x0002
40
41 typedef struct
42 { int channels, blocksize, samplesperblock, blocks, dataremaining ;
43 int blockcount ;
44 sf_count_t samplecount ;
45 short *samples ;
46 unsigned char *block ;
47 #if HAVE_FLEXIBLE_ARRAY
48 short dummydata [] ; /* ISO C99 struct flexible array. */
49 #else
50 short dummydata [0] ; /* This is a hack an might not work. */
51 #endif
52 } MSADPCM_PRIVATE ;
53
54 /*============================================================================================
55 ** MS ADPCM static data and functions.
56 */
57
58 static int AdaptationTable [] =
59 { 230, 230, 230, 230, 307, 409, 512, 614,
60 768, 614, 512, 409, 307, 230, 230, 230
61 } ;
62
63 /* TODO : The first 7 coef's are are always hardcode and must
64 appear in the actual WAVE file. They should be read in
65 in case a sound program added extras to the list. */
66
67 static int AdaptCoeff1 [MSADPCM_ADAPT_COEFF_COUNT] =
68 { 256, 512, 0, 192, 240, 460, 392
69 } ;
70
71 static int AdaptCoeff2 [MSADPCM_ADAPT_COEFF_COUNT] =
72 { 0, -256, 0, 64, 0, -208, -232
73 } ;
74
75 /*============================================================================================
76 ** MS ADPCM Block Layout.
77 ** ======================
78 ** Block is usually 256, 512 or 1024 bytes depending on sample rate.
79 ** For a mono file, the block is laid out as follows:
80 ** byte purpose
81 ** 0 block predictor [0..6]
82 ** 1,2 initial idelta (positive)
83 ** 3,4 sample 1
84 ** 5,6 sample 0
85 ** 7..n packed bytecodes
86 **
87 ** For a stereo file, the block is laid out as follows:
88 ** byte purpose
89 ** 0 block predictor [0..6] for left channel
90 ** 1 block predictor [0..6] for right channel
91 ** 2,3 initial idelta (positive) for left channel
92 ** 4,5 initial idelta (positive) for right channel
93 ** 6,7 sample 1 for left channel
94 ** 8,9 sample 1 for right channel
95 ** 10,11 sample 0 for left channel
96 ** 12,13 sample 0 for right channel
97 ** 14..n packed bytecodes
98 */
99
100 /*============================================================================================
101 ** Static functions.
102 */
103
104 static int msadpcm_decode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) ;
105 static sf_count_t msadpcm_read_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len) ;
106
107 static int msadpcm_encode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) ;
108 static sf_count_t msadpcm_write_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, const short *ptr, int len) ;
109
110 static sf_count_t msadpcm_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
111 static sf_count_t msadpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
112 static sf_count_t msadpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
113 static sf_count_t msadpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
114
115 static sf_count_t msadpcm_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
116 static sf_count_t msadpcm_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
117 static sf_count_t msadpcm_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
118 static sf_count_t msadpcm_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
119
120 static sf_count_t msadpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
121 static int msadpcm_close (SF_PRIVATE *psf) ;
122
123 static void choose_predictor (unsigned int channels, short *data, int *bpred, int *idelta) ;
124
125 /*============================================================================================
126 ** MS ADPCM Read Functions.
127 */
128
129 int
130 wav_w64_msadpcm_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
131 { MSADPCM_PRIVATE *pms ;
132 unsigned int pmssize ;
133 int count ;
134
135 if (psf->codec_data != NULL)
136 { psf_log_printf (psf, "*** psf->codec_data is not NULL.\n") ;
137 return SFE_INTERNAL ;
138 } ;
139
140 if (psf->file.mode == SFM_WRITE)
141 samplesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
142
143 pmssize = sizeof (MSADPCM_PRIVATE) + blockalign + 3 * psf->sf.channels * samplesperblock ;
144
145 if (! (psf->codec_data = calloc (1, pmssize)))
146 return SFE_MALLOC_FAILED ;
147 pms = (MSADPCM_PRIVATE*) psf->codec_data ;
148
149 pms->samples = pms->dummydata ;
150 pms->block = (unsigned char*) (pms->dummydata + psf->sf.channels * samplesperblock) ;
151
152 pms->channels = psf->sf.channels ;
153 pms->blocksize = blockalign ;
154 pms->samplesperblock = samplesperblock ;
155
156 if (pms->blocksize == 0)
157 { psf_log_printf (psf, "*** Error : pms->blocksize should not be zero.\n") ;
158 return SFE_INTERNAL ;
159 } ;
160
161 if (psf->file.mode == SFM_READ)
162 { pms->dataremaining = psf->datalength ;
163
164 if (psf->datalength % pms->blocksize)
165 pms->blocks = psf->datalength / pms->blocksize + 1 ;
166 else
167 pms->blocks = psf->datalength / pms->blocksize ;
168
169 count = 2 * (pms->blocksize - 6 * pms->channels) / pms->channels ;
170 if (pms->samplesperblock != count)
171 { psf_log_printf (psf, "*** Error : samplesperblock should be %d.\n", count) ;
172 return SFE_INTERNAL ;
173 } ;
174
175 psf->sf.frames = (psf->datalength / pms->blocksize) * pms->samplesperblock ;
176
177 psf_log_printf (psf, " bpred idelta\n") ;
178
179 msadpcm_decode_block (psf, pms) ;
180
181 psf->read_short = msadpcm_read_s ;
182 psf->read_int = msadpcm_read_i ;
183 psf->read_float = msadpcm_read_f ;
184 psf->read_double = msadpcm_read_d ;
185 } ;
186
187 if (psf->file.mode == SFM_WRITE)
188 { pms->samples = pms->dummydata ;
189
190 pms->samplecount = 0 ;
191
192 psf->write_short = msadpcm_write_s ;
193 psf->write_int = msadpcm_write_i ;
194 psf->write_float = msadpcm_write_f ;
195 psf->write_double = msadpcm_write_d ;
196 } ;
197
198 psf->codec_close = msadpcm_close ;
199 psf->seek = msadpcm_seek ;
200
201 return 0 ;
202 } /* wav_w64_msadpcm_init */
203
204 static int
205 msadpcm_decode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms)
206 { int chan, k, blockindx, sampleindx ;
207 short bytecode, bpred [2], chan_idelta [2] ;
208
209 int predict ;
210 int current ;
211 int idelta ;
212
213 pms->blockcount ++ ;
214 pms->samplecount = 0 ;
215
216 if (pms->blockcount > pms->blocks)
217 { memset (pms->samples, 0, pms->samplesperblock * pms->channels) ;
218 return 1 ;
219 } ;
220
221 if ((k = psf_fread (pms->block, 1, pms->blocksize, psf)) != pms->blocksize)
222 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pms->blocksize) ;
223
224 /* Read and check the block header. */
225
226 if (pms->channels == 1)
227 { bpred [0] = pms->block [0] ;
228
229 if (bpred [0] >= 7)
230 psf_log_printf (psf, "MS ADPCM synchronisation error (%d).\n", bpred [0]) ;
231
232 chan_idelta [0] = pms->block [1] | (pms->block [2] << 8) ;
233 chan_idelta [1] = 0 ;
234
235 psf_log_printf (psf, "(%d) (%d)\n", bpred [0], chan_idelta [0]) ;
236
237 pms->samples [1] = pms->block [3] | (pms->block [4] << 8) ;
238 pms->samples [0] = pms->block [5] | (pms->block [6] << 8) ;
239 blockindx = 7 ;
240 }
241 else
242 { bpred [0] = pms->block [0] ;
243 bpred [1] = pms->block [1] ;
244
245 if (bpred [0] >= 7 || bpred [1] >= 7)
246 psf_log_printf (psf, "MS ADPCM synchronisation error (%d %d).\n", bpred [0], bpred [1]) ;
247
248 chan_idelta [0] = pms->block [2] | (pms->block [3] << 8) ;
249 chan_idelta [1] = pms->block [4] | (pms->block [5] << 8) ;
250
251 psf_log_printf (psf, "(%d, %d) (%d, %d)\n", bpred [0], bpred [1], chan_idelta [0], chan_idelta [1]) ;
252
253 pms->samples [2] = pms->block [6] | (pms->block [7] << 8) ;
254 pms->samples [3] = pms->block [8] | (pms->block [9] << 8) ;
255
256 pms->samples [0] = pms->block [10] | (pms->block [11] << 8) ;
257 pms->samples [1] = pms->block [12] | (pms->block [13] << 8) ;
258
259 blockindx = 14 ;
260 } ;
261
262 /*--------------------------------------------------------
263 This was left over from a time when calculations were done
264 as ints rather than shorts. Keep this around as a reminder
265 in case I ever find a file which decodes incorrectly.
266
267 if (chan_idelta [0] & 0x8000)
268 chan_idelta [0] -= 0x10000 ;
269 if (chan_idelta [1] & 0x8000)
270 chan_idelta [1] -= 0x10000 ;
271 --------------------------------------------------------*/
272
273 /* Pull apart the packed 4 bit samples and store them in their
274 ** correct sample positions.
275 */
276
277 sampleindx = 2 * pms->channels ;
278 while (blockindx < pms->blocksize)
279 { bytecode = pms->block [blockindx++] ;
280 pms->samples [sampleindx++] = (bytecode >> 4) & 0x0F ;
281 pms->samples [sampleindx++] = bytecode & 0x0F ;
282 } ;
283
284 /* Decode the encoded 4 bit samples. */
285
286 for (k = 2 * pms->channels ; k < (pms->samplesperblock * pms->channels) ; k ++)
287 { chan = (pms->channels > 1) ? (k % 2) : 0 ;
288
289 bytecode = pms->samples [k] & 0xF ;
290
291 /* Compute next Adaptive Scale Factor (ASF) */
292 idelta = chan_idelta [chan] ;
293 chan_idelta [chan] = (AdaptationTable [bytecode] * idelta) >> 8 ; /* => / 256 => FIXED_POINT_ADAPTATION_BASE == 256 */
294 if (chan_idelta [chan] < 16)
295 chan_idelta [chan] = 16 ;
296 if (bytecode & 0x8)
297 bytecode -= 0x10 ;
298
299 predict = ((pms->samples [k - pms->channels] * AdaptCoeff1 [bpred [chan]])
300 + (pms->samples [k - 2 * pms->channels] * AdaptCoeff2 [bpred [chan]])) >> 8 ; /* => / 256 => FIXED_POINT_COEFF_BASE == 256 */
301 current = (bytecode * idelta) + predict ;
302
303 if (current > 32767)
304 current = 32767 ;
305 else if (current < -32768)
306 current = -32768 ;
307
308 pms->samples [k] = current ;
309 } ;
310
311 return 1 ;
312 } /* msadpcm_decode_block */
313
314 static sf_count_t
315 msadpcm_read_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len)
316 { int count, total = 0, indx = 0 ;
317
318 while (indx < len)
319 { if (pms->blockcount >= pms->blocks && pms->samplecount >= pms->samplesperblock)
320 { memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ;
321 return total ;
322 } ;
323
324 if (pms->samplecount >= pms->samplesperblock)
325 msadpcm_decode_block (psf, pms) ;
326
327 count = (pms->samplesperblock - pms->samplecount) * pms->channels ;
328 count = (len - indx > count) ? count : len - indx ;
329
330 memcpy (&(ptr [indx]), &(pms->samples [pms->samplecount * pms->channels]), count * sizeof (short)) ;
331 indx += count ;
332 pms->samplecount += count / pms->channels ;
333 total = indx ;
334 } ;
335
336 return total ;
337 } /* msadpcm_read_block */
338
339 static sf_count_t
340 msadpcm_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
341 { MSADPCM_PRIVATE *pms ;
342 int readcount, count ;
343 sf_count_t total = 0 ;
344
345 if (! psf->codec_data)
346 return 0 ;
347 pms = (MSADPCM_PRIVATE*) psf->codec_data ;
348
349 while (len > 0)
350 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
351
352 count = msadpcm_read_block (psf, pms, ptr, readcount) ;
353
354 total += count ;
355 len -= count ;
356 if (count != readcount)
357 break ;
358 } ;
359
360 return total ;
361 } /* msadpcm_read_s */
362
363 static sf_count_t
364 msadpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
365 { MSADPCM_PRIVATE *pms ;
366 short *sptr ;
367 int k, bufferlen, readcount = 0, count ;
368 sf_count_t total = 0 ;
369
370 if (! psf->codec_data)
371 return 0 ;
372 pms = (MSADPCM_PRIVATE*) psf->codec_data ;
373
374 sptr = psf->u.sbuf ;
375 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
376 while (len > 0)
377 { readcount = (len >= bufferlen) ? bufferlen : len ;
378 count = msadpcm_read_block (psf, pms, sptr, readcount) ;
379 for (k = 0 ; k < readcount ; k++)
380 ptr [total + k] = sptr [k] << 16 ;
381 total += count ;
382 len -= readcount ;
383 if (count != readcount)
384 break ;
385 } ;
386 return total ;
387 } /* msadpcm_read_i */
388
389 static sf_count_t
390 msadpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
391 { MSADPCM_PRIVATE *pms ;
392 short *sptr ;
393 int k, bufferlen, readcount = 0, count ;
394 sf_count_t total = 0 ;
395 float normfact ;
396
397 if (! psf->codec_data)
398 return 0 ;
399 pms = (MSADPCM_PRIVATE*) psf->codec_data ;
400
401 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
402 sptr = psf->u.sbuf ;
403 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
404 while (len > 0)
405 { readcount = (len >= bufferlen) ? bufferlen : len ;
406 count = msadpcm_read_block (psf, pms, sptr, readcount) ;
407 for (k = 0 ; k < readcount ; k++)
408 ptr [total + k] = normfact * (float) (sptr [k]) ;
409 total += count ;
410 len -= readcount ;
411 if (count != readcount)
412 break ;
413 } ;
414 return total ;
415 } /* msadpcm_read_f */
416
417 static sf_count_t
418 msadpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
419 { MSADPCM_PRIVATE *pms ;
420 short *sptr ;
421 int k, bufferlen, readcount = 0, count ;
422 sf_count_t total = 0 ;
423 double normfact ;
424
425 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
426
427 if (! psf->codec_data)
428 return 0 ;
429 pms = (MSADPCM_PRIVATE*) psf->codec_data ;
430
431 sptr = psf->u.sbuf ;
432 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
433 while (len > 0)
434 { readcount = (len >= bufferlen) ? bufferlen : len ;
435 count = msadpcm_read_block (psf, pms, sptr, readcount) ;
436 for (k = 0 ; k < readcount ; k++)
437 ptr [total + k] = normfact * (double) (sptr [k]) ;
438 total += count ;
439 len -= readcount ;
440 if (count != readcount)
441 break ;
442 } ;
443 return total ;
444 } /* msadpcm_read_d */
445
446 static sf_count_t
447 msadpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
448 { MSADPCM_PRIVATE *pms ;
449 int newblock, newsample ;
450
451 if (! psf->codec_data)
452 return 0 ;
453 pms = (MSADPCM_PRIVATE*) psf->codec_data ;
454
455 if (psf->datalength < 0 || psf->dataoffset < 0)
456 { psf->error = SFE_BAD_SEEK ;
457 return PSF_SEEK_ERROR ;
458 } ;
459
460 if (offset == 0)
461 { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
462 pms->blockcount = 0 ;
463 msadpcm_decode_block (psf, pms) ;
464 pms->samplecount = 0 ;
465 return 0 ;
466 } ;
467
468 if (offset < 0 || offset > pms->blocks * pms->samplesperblock)
469 { psf->error = SFE_BAD_SEEK ;
470 return PSF_SEEK_ERROR ;
471 } ;
472
473 newblock = offset / pms->samplesperblock ;
474 newsample = offset % pms->samplesperblock ;
475
476 if (mode == SFM_READ)
477 { psf_fseek (psf, psf->dataoffset + newblock * pms->blocksize, SEEK_SET) ;
478 pms->blockcount = newblock ;
479 msadpcm_decode_block (psf, pms) ;
480 pms->samplecount = newsample ;
481 }
482 else
483 { /* What to do about write??? */
484 psf->error = SFE_BAD_SEEK ;
485 return PSF_SEEK_ERROR ;
486 } ;
487
488 return newblock * pms->samplesperblock + newsample ;
489 } /* msadpcm_seek */
490
491 /*==========================================================================================
492 ** MS ADPCM Write Functions.
493 */
494
495 void
496 msadpcm_write_adapt_coeffs (SF_PRIVATE *psf)
497 { int k ;
498
499 for (k = 0 ; k < MSADPCM_ADAPT_COEFF_COUNT ; k++)
500 psf_binheader_writef (psf, "22", AdaptCoeff1 [k], AdaptCoeff2 [k]) ;
501 } /* msadpcm_write_adapt_coeffs */
502
503 /*==========================================================================================
504 */
505
506 static int
507 msadpcm_encode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms)
508 { unsigned int blockindx ;
509 unsigned char byte ;
510 int chan, k, predict, bpred [2], idelta [2], errordelta, newsamp ;
511
512 choose_predictor (pms->channels, pms->samples, bpred, idelta) ;
513
514 /* Write the block header. */
515
516 if (pms->channels == 1)
517 { pms->block [0] = bpred [0] ;
518 pms->block [1] = idelta [0] & 0xFF ;
519 pms->block [2] = idelta [0] >> 8 ;
520 pms->block [3] = pms->samples [1] & 0xFF ;
521 pms->block [4] = pms->samples [1] >> 8 ;
522 pms->block [5] = pms->samples [0] & 0xFF ;
523 pms->block [6] = pms->samples [0] >> 8 ;
524
525 blockindx = 7 ;
526 byte = 0 ;
527
528 /* Encode the samples as 4 bit. */
529
530 for (k = 2 ; k < pms->samplesperblock ; k++)
531 { predict = (pms->samples [k-1] * AdaptCoeff1 [bpred [0]] + pms->samples [k-2] * AdaptCoeff2 [bpred [0]]) >> 8 ;
532 errordelta = (pms->samples [k] - predict) / idelta [0] ;
533 if (errordelta < -8)
534 errordelta = -8 ;
535 else if (errordelta > 7)
536 errordelta = 7 ;
537 newsamp = predict + (idelta [0] * errordelta) ;
538 if (newsamp > 32767)
539 newsamp = 32767 ;
540 else if (newsamp < -32768)
541 newsamp = -32768 ;
542 if (errordelta < 0)
543 errordelta += 0x10 ;
544
545 byte = (byte << 4) | (errordelta & 0xF) ;
546 if (k % 2)
547 { pms->block [blockindx++] = byte ;
548 byte = 0 ;
549 } ;
550
551 idelta [0] = (idelta [0] * AdaptationTable [errordelta]) >> 8 ;
552 if (idelta [0] < 16)
553 idelta [0] = 16 ;
554 pms->samples [k] = newsamp ;
555 } ;
556 }
557 else
558 { /* Stereo file. */
559 pms->block [0] = bpred [0] ;
560 pms->block [1] = bpred [1] ;
561
562 pms->block [2] = idelta [0] & 0xFF ;
563 pms->block [3] = idelta [0] >> 8 ;
564 pms->block [4] = idelta [1] & 0xFF ;
565 pms->block [5] = idelta [1] >> 8 ;
566
567 pms->block [6] = pms->samples [2] & 0xFF ;
568 pms->block [7] = pms->samples [2] >> 8 ;
569 pms->block [8] = pms->samples [3] & 0xFF ;
570 pms->block [9] = pms->samples [3] >> 8 ;
571
572 pms->block [10] = pms->samples [0] & 0xFF ;
573 pms->block [11] = pms->samples [0] >> 8 ;
574 pms->block [12] = pms->samples [1] & 0xFF ;
575 pms->block [13] = pms->samples [1] >> 8 ;
576
577 blockindx = 14 ;
578 byte = 0 ;
579 chan = 1 ;
580
581 for (k = 4 ; k < 2 * pms->samplesperblock ; k++)
582 { chan = k & 1 ;
583
584 predict = (pms->samples [k-2] * AdaptCoeff1 [bpred [chan]] + pms->samples [k-4] * AdaptCoeff2 [bpred [chan]]) >> 8 ;
585 errordelta = (pms->samples [k] - predict) / idelta [chan] ;
586
587
588 if (errordelta < -8)
589 errordelta = -8 ;
590 else if (errordelta > 7)
591 errordelta = 7 ;
592 newsamp = predict + (idelta [chan] * errordelta) ;
593 if (newsamp > 32767)
594 newsamp = 32767 ;
595 else if (newsamp < -32768)
596 newsamp = -32768 ;
597 if (errordelta < 0)
598 errordelta += 0x10 ;
599
600 byte = (byte << 4) | (errordelta & 0xF) ;
601
602 if (chan)
603 { pms->block [blockindx++] = byte ;
604 byte = 0 ;
605 } ;
606
607 idelta [chan] = (idelta [chan] * AdaptationTable [errordelta]) >> 8 ;
608 if (idelta [chan] < 16)
609 idelta [chan] = 16 ;
610 pms->samples [k] = newsamp ;
611 } ;
612 } ;
613
614 /* Write the block to disk. */
615
616 if ((k = psf_fwrite (pms->block, 1, pms->blocksize, psf)) != pms->blocksize)
617 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pms->blocksize) ;
618
619 memset (pms->samples, 0, pms->samplesperblock * sizeof (short)) ;
620
621 pms->blockcount ++ ;
622 pms->samplecount = 0 ;
623
624 return 1 ;
625 } /* msadpcm_encode_block */
626
627 static sf_count_t
628 msadpcm_write_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, const short *ptr, int len)
629 { int count, total = 0, indx = 0 ;
630
631 while (indx < len)
632 { count = (pms->samplesperblock - pms->samplecount) * pms->channels ;
633
634 if (count > len - indx)
635 count = len - indx ;
636
637 memcpy (&(pms->samples [pms->samplecount * pms->channels]), &(ptr [total]), count * sizeof (short)) ;
638 indx += count ;
639 pms->samplecount += count / pms->channels ;
640 total = indx ;
641
642 if (pms->samplecount >= pms->samplesperblock)
643 msadpcm_encode_block (psf, pms) ;
644 } ;
645
646 return total ;
647 } /* msadpcm_write_block */
648
649 static sf_count_t
650 msadpcm_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
651 { MSADPCM_PRIVATE *pms ;
652 int writecount, count ;
653 sf_count_t total = 0 ;
654
655 if (! psf->codec_data)
656 return 0 ;
657 pms = (MSADPCM_PRIVATE*) psf->codec_data ;
658
659 while (len > 0)
660 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
661
662 count = msadpcm_write_block (psf, pms, ptr, writecount) ;
663
664 total += count ;
665 len -= count ;
666 if (count != writecount)
667 break ;
668 } ;
669
670 return total ;
671 } /* msadpcm_write_s */
672
673 static sf_count_t
674 msadpcm_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
675 { MSADPCM_PRIVATE *pms ;
676 short *sptr ;
677 int k, bufferlen, writecount, count ;
678 sf_count_t total = 0 ;
679
680 if (! psf->codec_data)
681 return 0 ;
682 pms = (MSADPCM_PRIVATE*) psf->codec_data ;
683
684 sptr = psf->u.sbuf ;
685 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
686 while (len > 0)
687 { writecount = (len >= bufferlen) ? bufferlen : len ;
688 for (k = 0 ; k < writecount ; k++)
689 sptr [k] = ptr [total + k] >> 16 ;
690 count = msadpcm_write_block (psf, pms, sptr, writecount) ;
691 total += count ;
692 len -= writecount ;
693 if (count != writecount)
694 break ;
695 } ;
696 return total ;
697 } /* msadpcm_write_i */
698
699 static sf_count_t
700 msadpcm_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
701 { MSADPCM_PRIVATE *pms ;
702 short *sptr ;
703 int k, bufferlen, writecount, count ;
704 sf_count_t total = 0 ;
705 float normfact ;
706
707 if (! psf->codec_data)
708 return 0 ;
709 pms = (MSADPCM_PRIVATE*) psf->codec_data ;
710
711 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
712
713 sptr = psf->u.sbuf ;
714 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
715 while (len > 0)
716 { writecount = (len >= bufferlen) ? bufferlen : len ;
717 for (k = 0 ; k < writecount ; k++)
718 sptr [k] = lrintf (normfact * ptr [total + k]) ;
719 count = msadpcm_write_block (psf, pms, sptr, writecount) ;
720 total += count ;
721 len -= writecount ;
722 if (count != writecount)
723 break ;
724 } ;
725 return total ;
726 } /* msadpcm_write_f */
727
728 static sf_count_t
729 msadpcm_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
730 { MSADPCM_PRIVATE *pms ;
731 short *sptr ;
732 int k, bufferlen, writecount, count ;
733 sf_count_t total = 0 ;
734 double normfact ;
735
736 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
737
738 if (! psf->codec_data)
739 return 0 ;
740 pms = (MSADPCM_PRIVATE*) psf->codec_data ;
741
742 sptr = psf->u.sbuf ;
743 bufferlen = ARRAY_LEN (psf->u.sbuf) ;
744 while (len > 0)
745 { writecount = (len >= bufferlen) ? bufferlen : len ;
746 for (k = 0 ; k < writecount ; k++)
747 sptr [k] = lrint (normfact * ptr [total + k]) ;
748 count = msadpcm_write_block (psf, pms, sptr, writecount) ;
749 total += count ;
750 len -= writecount ;
751 if (count != writecount)
752 break ;
753 } ;
754 return total ;
755 } /* msadpcm_write_d */
756
757 /*========================================================================================
758 */
759
760 static int
761 msadpcm_close (SF_PRIVATE *psf)
762 { MSADPCM_PRIVATE *pms ;
763
764 pms = (MSADPCM_PRIVATE*) psf->codec_data ;
765
766 if (psf->file.mode == SFM_WRITE)
767 { /* Now we know static int for certain the length of the file we can
768 ** re-write the header.
769 */
770
771 if (pms->samplecount && pms->samplecount < pms->samplesperblock)
772 msadpcm_encode_block (psf, pms) ;
773 } ;
774
775 return 0 ;
776 } /* msadpcm_close */
777
778 /*========================================================================================
779 ** Static functions.
780 */
781
782 /*----------------------------------------------------------------------------------------
783 ** Choosing the block predictor.
784 ** Each block requires a predictor and an idelta for each channel.
785 ** The predictor is in the range [0..6] which is an indx into the two AdaptCoeff tables.
786 ** The predictor is chosen by trying all of the possible predictors on a small set of
787 ** samples at the beginning of the block. The predictor with the smallest average
788 ** abs (idelta) is chosen as the best predictor for this block.
789 ** The value of idelta is chosen to to give a 4 bit code value of +/- 4 (approx. half the
790 ** max. code value). If the average abs (idelta) is zero, the sixth predictor is chosen.
791 ** If the value of idelta is less then 16 it is set to 16.
792 **
793 ** Microsoft uses an IDELTA_COUNT (number of sample pairs used to choose best predictor)
794 ** value of 3. The best possible results would be obtained by using all the samples to
795 ** choose the predictor.
796 */
797
798 #define IDELTA_COUNT 3
799
800 static void
801 choose_predictor (unsigned int channels, short *data, int *block_pred, int *idelta)
802 { unsigned int chan, k, bpred, idelta_sum, best_bpred, best_idelta ;
803
804 for (chan = 0 ; chan < channels ; chan++)
805 { best_bpred = best_idelta = 0 ;
806
807 for (bpred = 0 ; bpred < 7 ; bpred++)
808 { idelta_sum = 0 ;
809 for (k = 2 ; k < 2 + IDELTA_COUNT ; k++)
810 idelta_sum += abs (data [k * channels] - ((data [(k - 1) * channels] * AdaptCoeff1 [bpred] + data [(k - 2) * channels] * AdaptCoeff2 [bpred]) >> 8)) ;
811 idelta_sum /= (4 * IDELTA_COUNT) ;
812
813 if (bpred == 0 || idelta_sum < best_idelta)
814 { best_bpred = bpred ;
815 best_idelta = idelta_sum ;
816 } ;
817
818 if (! idelta_sum)
819 { best_bpred = bpred ;
820 best_idelta = 16 ;
821 break ;
822 } ;
823
824 } ; /* for bpred ... */
825 if (best_idelta < 16)
826 best_idelta = 16 ;
827
828 block_pred [chan] = best_bpred ;
829 idelta [chan] = best_idelta ;
830 } ;
831
832 return ;
833 } /* choose_predictor */
834