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