/project/jni/sndfile/src/ms_adpcm.c

https://github.com/aichunyu/FFPlayer · C · 834 lines · 565 code · 169 blank · 100 comment · 124 complexity · e55d299f8095fff85a3317f4f844077c MD5 · raw file

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