/project/jni/sndfile/src/sds.c

https://github.com/aichunyu/FFPlayer · C · 1005 lines · 727 code · 232 blank · 46 comment · 164 complexity · 7c172188a93753f2ad8f73c9274138d4 MD5 · raw file

  1. /*
  2. ** Copyright (C) 2002-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 <fcntl.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <math.h>
  25. #include "sndfile.h"
  26. #include "sfendian.h"
  27. #include "common.h"
  28. /*------------------------------------------------------------------------------
  29. */
  30. #define SDS_DATA_OFFSET 0x15
  31. #define SDS_BLOCK_SIZE 127
  32. #define SDS_AUDIO_BYTES_PER_BLOCK 120
  33. #define SDS_3BYTE_TO_INT_DECODE(x) (((x) & 0x7F) | (((x) & 0x7F00) >> 1) | (((x) & 0x7F0000) >> 2))
  34. #define SDS_INT_TO_3BYTE_ENCODE(x) (((x) & 0x7F) | (((x) << 1) & 0x7F00) | (((x) << 2) & 0x7F0000))
  35. /*------------------------------------------------------------------------------
  36. ** Typedefs.
  37. */
  38. typedef struct tag_SDS_PRIVATE
  39. { int bitwidth, frames ;
  40. int samplesperblock, total_blocks ;
  41. int (*reader) (SF_PRIVATE *psf, struct tag_SDS_PRIVATE *psds) ;
  42. int (*writer) (SF_PRIVATE *psf, struct tag_SDS_PRIVATE *psds) ;
  43. int read_block, read_count ;
  44. unsigned char read_data [SDS_BLOCK_SIZE] ;
  45. int read_samples [SDS_BLOCK_SIZE / 2] ; /* Maximum samples per block */
  46. int write_block, write_count ;
  47. int total_written ;
  48. unsigned char write_data [SDS_BLOCK_SIZE] ;
  49. int write_samples [SDS_BLOCK_SIZE / 2] ; /* Maximum samples per block */
  50. } SDS_PRIVATE ;
  51. /*------------------------------------------------------------------------------
  52. ** Private static functions.
  53. */
  54. static int sds_close (SF_PRIVATE *psf) ;
  55. static int sds_write_header (SF_PRIVATE *psf, int calc_length) ;
  56. static int sds_read_header (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
  57. static int sds_init (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
  58. static sf_count_t sds_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  59. static sf_count_t sds_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  60. static sf_count_t sds_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  61. static sf_count_t sds_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  62. static sf_count_t sds_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  63. static sf_count_t sds_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  64. static sf_count_t sds_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  65. static sf_count_t sds_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
  66. static sf_count_t sds_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
  67. static int sds_2byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
  68. static int sds_3byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
  69. static int sds_4byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
  70. static int sds_read (SF_PRIVATE *psf, SDS_PRIVATE *psds, int *iptr, int readcount) ;
  71. static int sds_2byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
  72. static int sds_3byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
  73. static int sds_4byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
  74. static int sds_write (SF_PRIVATE *psf, SDS_PRIVATE *psds, const int *iptr, int writecount) ;
  75. /*------------------------------------------------------------------------------
  76. ** Public function.
  77. */
  78. int
  79. sds_open (SF_PRIVATE *psf)
  80. { SDS_PRIVATE *psds ;
  81. int error = 0 ;
  82. /* Hmmmm, need this here to pass update_header_test. */
  83. psf->sf.frames = 0 ;
  84. if (! (psds = calloc (1, sizeof (SDS_PRIVATE))))
  85. return SFE_MALLOC_FAILED ;
  86. psf->codec_data = psds ;
  87. if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
  88. { if ((error = sds_read_header (psf, psds)))
  89. return error ;
  90. } ;
  91. if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_SDS)
  92. return SFE_BAD_OPEN_FORMAT ;
  93. if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
  94. { if (sds_write_header (psf, SF_FALSE))
  95. return psf->error ;
  96. psf->write_header = sds_write_header ;
  97. psf_fseek (psf, SDS_DATA_OFFSET, SEEK_SET) ;
  98. } ;
  99. if ((error = sds_init (psf, psds)) != 0)
  100. return error ;
  101. psf->seek = sds_seek ;
  102. psf->container_close = sds_close ;
  103. psf->blockwidth = 0 ;
  104. return error ;
  105. } /* sds_open */
  106. /*------------------------------------------------------------------------------
  107. */
  108. static int
  109. sds_close (SF_PRIVATE *psf)
  110. {
  111. if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
  112. { SDS_PRIVATE *psds ;
  113. if ((psds = (SDS_PRIVATE *) psf->codec_data) == NULL)
  114. { psf_log_printf (psf, "*** Bad psf->codec_data ptr.\n") ;
  115. return SFE_INTERNAL ;
  116. } ;
  117. if (psds->write_count > 0)
  118. { memset (&(psds->write_data [psds->write_count]), 0, (psds->samplesperblock - psds->write_count) * sizeof (int)) ;
  119. psds->writer (psf, psds) ;
  120. } ;
  121. sds_write_header (psf, SF_TRUE) ;
  122. } ;
  123. return 0 ;
  124. } /* sds_close */
  125. static int
  126. sds_init (SF_PRIVATE *psf, SDS_PRIVATE *psds)
  127. {
  128. if (psds->bitwidth < 8 || psds->bitwidth > 28)
  129. return (psf->error = SFE_SDS_BAD_BIT_WIDTH) ;
  130. if (psds->bitwidth < 14)
  131. { psds->reader = sds_2byte_read ;
  132. psds->writer = sds_2byte_write ;
  133. psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / 2 ;
  134. }
  135. else if (psds->bitwidth < 21)
  136. { psds->reader = sds_3byte_read ;
  137. psds->writer = sds_3byte_write ;
  138. psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / 3 ;
  139. }
  140. else
  141. { psds->reader = sds_4byte_read ;
  142. psds->writer = sds_4byte_write ;
  143. psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / 4 ;
  144. } ;
  145. if (psf->file.mode == SFM_READ || psf->file.mode == SFM_RDWR)
  146. { psf->read_short = sds_read_s ;
  147. psf->read_int = sds_read_i ;
  148. psf->read_float = sds_read_f ;
  149. psf->read_double = sds_read_d ;
  150. /* Read first block. */
  151. psds->reader (psf, psds) ;
  152. } ;
  153. if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
  154. { psf->write_short = sds_write_s ;
  155. psf->write_int = sds_write_i ;
  156. psf->write_float = sds_write_f ;
  157. psf->write_double = sds_write_d ;
  158. } ;
  159. return 0 ;
  160. } /* sds_init */
  161. static int
  162. sds_read_header (SF_PRIVATE *psf, SDS_PRIVATE *psds)
  163. { unsigned char channel, bitwidth, loop_type, byte ;
  164. unsigned short sample_no, marker ;
  165. unsigned int samp_period, data_length, sustain_loop_start, sustain_loop_end ;
  166. int bytesread, blockcount ;
  167. /* Set position to start of file to begin reading header. */
  168. bytesread = psf_binheader_readf (psf, "pE211", 0, &marker, &channel, &byte) ;
  169. if (marker != 0xF07E || byte != 0x01)
  170. return SFE_SDS_NOT_SDS ;
  171. bytesread += psf_binheader_readf (psf, "e2", &sample_no) ;
  172. sample_no = SDS_3BYTE_TO_INT_DECODE (sample_no) ;
  173. psf_log_printf (psf, "Midi Sample Dump Standard (.sds)\nF07E\n"
  174. " Midi Channel : %d\n Sample Number : %d\n",
  175. channel, sample_no) ;
  176. bytesread += psf_binheader_readf (psf, "e13", &bitwidth, &samp_period) ;
  177. samp_period = SDS_3BYTE_TO_INT_DECODE (samp_period) ;
  178. psds->bitwidth = bitwidth ;
  179. if (psds->bitwidth > 1)
  180. psf_log_printf (psf, " Bit Width : %d\n", psds->bitwidth) ;
  181. else
  182. { psf_log_printf (psf, " Bit Width : %d (should be > 1)\n", psds->bitwidth) ;
  183. return SFE_SDS_BAD_BIT_WIDTH ;
  184. } ;
  185. if (samp_period > 0)
  186. { psf->sf.samplerate = 1000000000 / samp_period ;
  187. psf_log_printf (psf, " Sample Period : %d\n"
  188. " Sample Rate : %d\n",
  189. samp_period, psf->sf.samplerate) ;
  190. }
  191. else
  192. { psf->sf.samplerate = 16000 ;
  193. psf_log_printf (psf, " Sample Period : %d (should be > 0)\n"
  194. " Sample Rate : %d (guessed)\n",
  195. samp_period, psf->sf.samplerate) ;
  196. } ;
  197. bytesread += psf_binheader_readf (psf, "e3331", &data_length, &sustain_loop_start, &sustain_loop_end, &loop_type) ;
  198. data_length = SDS_3BYTE_TO_INT_DECODE (data_length) ;
  199. psf->sf.frames = psds->frames = data_length ;
  200. sustain_loop_start = SDS_3BYTE_TO_INT_DECODE (sustain_loop_start) ;
  201. sustain_loop_end = SDS_3BYTE_TO_INT_DECODE (sustain_loop_end) ;
  202. psf_log_printf (psf, " Sustain Loop\n"
  203. " Start : %d\n"
  204. " End : %d\n"
  205. " Loop Type : %d\n",
  206. sustain_loop_start, sustain_loop_end, loop_type) ;
  207. psf->dataoffset = SDS_DATA_OFFSET ;
  208. psf->datalength = psf->filelength - psf->dataoffset ;
  209. bytesread += psf_binheader_readf (psf, "1", &byte) ;
  210. if (byte != 0xF7)
  211. psf_log_printf (psf, "bad end : %X\n", byte & 0xFF) ;
  212. for (blockcount = 0 ; bytesread < psf->filelength ; blockcount++)
  213. {
  214. bytesread += psf_fread (&marker, 1, 2, psf) ;
  215. if (marker == 0)
  216. break ;
  217. psf_fseek (psf, SDS_BLOCK_SIZE - 2, SEEK_CUR) ;
  218. bytesread += SDS_BLOCK_SIZE - 2 ;
  219. } ;
  220. psf_log_printf (psf, "\nBlocks : %d\n", blockcount) ;
  221. psds->total_blocks = blockcount ;
  222. psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / ((psds->bitwidth + 6) / 7) ;
  223. psf_log_printf (psf, "Samples/Block : %d\n", psds->samplesperblock) ;
  224. psf_log_printf (psf, "Frames : %d\n", blockcount * psds->samplesperblock) ;
  225. /* Always Mono */
  226. psf->sf.channels = 1 ;
  227. psf->sf.sections = 1 ;
  228. /*
  229. ** Lie to the user about PCM bit width. Always round up to
  230. ** the next multiple of 8.
  231. */
  232. switch ((psds->bitwidth + 7) / 8)
  233. { case 1 :
  234. psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_S8 ;
  235. break ;
  236. case 2 :
  237. psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_16 ;
  238. break ;
  239. case 3 :
  240. psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_24 ;
  241. break ;
  242. case 4 :
  243. psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_32 ;
  244. break ;
  245. default :
  246. psf_log_printf (psf, "*** Weird byte width (%d)\n", (psds->bitwidth + 7) / 8) ;
  247. return SFE_SDS_BAD_BIT_WIDTH ;
  248. } ;
  249. psf_fseek (psf, SDS_DATA_OFFSET, SEEK_SET) ;
  250. return 0 ;
  251. } /* sds_read_header */
  252. static int
  253. sds_write_header (SF_PRIVATE *psf, int calc_length)
  254. { SDS_PRIVATE *psds ;
  255. sf_count_t current ;
  256. int samp_period, data_length, sustain_loop_start, sustain_loop_end ;
  257. unsigned char loop_type = 0 ;
  258. if ((psds = (SDS_PRIVATE *) psf->codec_data) == NULL)
  259. { psf_log_printf (psf, "*** Bad psf->codec_data ptr.\n") ;
  260. return SFE_INTERNAL ;
  261. } ;
  262. if (psf->pipeoffset > 0)
  263. return 0 ;
  264. current = psf_ftell (psf) ;
  265. if (calc_length)
  266. psf->sf.frames = psds->total_written ;
  267. if (psds->write_count > 0)
  268. { int current_count = psds->write_count ;
  269. int current_block = psds->write_block ;
  270. psds->writer (psf, psds) ;
  271. psf_fseek (psf, -1 * SDS_BLOCK_SIZE, SEEK_CUR) ;
  272. psds->write_count = current_count ;
  273. psds->write_block = current_block ;
  274. } ;
  275. /* Reset the current header length to zero. */
  276. psf->header [0] = 0 ;
  277. psf->headindex = 0 ;
  278. if (psf->is_pipe == SF_FALSE)
  279. psf_fseek (psf, 0, SEEK_SET) ;
  280. psf_binheader_writef (psf, "E211", 0xF07E, 0, 1) ;
  281. switch (SF_CODEC (psf->sf.format))
  282. { case SF_FORMAT_PCM_S8 :
  283. psds->bitwidth = 8 ;
  284. break ;
  285. case SF_FORMAT_PCM_16 :
  286. psds->bitwidth = 16 ;
  287. break ;
  288. case SF_FORMAT_PCM_24 :
  289. psds->bitwidth = 24 ;
  290. break ;
  291. default:
  292. return SFE_SDS_BAD_BIT_WIDTH ;
  293. } ;
  294. samp_period = SDS_INT_TO_3BYTE_ENCODE (1000000000 / psf->sf.samplerate) ;
  295. psf_binheader_writef (psf, "e213", 0, psds->bitwidth, samp_period) ;
  296. data_length = SDS_INT_TO_3BYTE_ENCODE (psds->total_written) ;
  297. sustain_loop_start = SDS_INT_TO_3BYTE_ENCODE (0) ;
  298. sustain_loop_end = SDS_INT_TO_3BYTE_ENCODE (0) ;
  299. psf_binheader_writef (psf, "e33311", data_length, sustain_loop_start, sustain_loop_end, loop_type, 0xF7) ;
  300. /* Header construction complete so write it out. */
  301. psf_fwrite (psf->header, psf->headindex, 1, psf) ;
  302. if (psf->error)
  303. return psf->error ;
  304. psf->dataoffset = psf->headindex ;
  305. psf->datalength = psds->write_block * SDS_BLOCK_SIZE ;
  306. if (current > 0)
  307. psf_fseek (psf, current, SEEK_SET) ;
  308. return psf->error ;
  309. } /* sds_write_header */
  310. /*------------------------------------------------------------------------------
  311. */
  312. static int
  313. sds_2byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds)
  314. { unsigned char *ucptr, checksum ;
  315. unsigned int sample ;
  316. int k ;
  317. psds->read_block ++ ;
  318. psds->read_count = 0 ;
  319. if (psds->read_block * psds->samplesperblock > psds->frames)
  320. { memset (psds->read_samples, 0, psds->samplesperblock * sizeof (int)) ;
  321. return 1 ;
  322. } ;
  323. if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
  324. psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
  325. if (psds->read_data [0] != 0xF0)
  326. { printf ("Error A : %02X\n", psds->read_data [0] & 0xFF) ;
  327. } ;
  328. checksum = psds->read_data [1] ;
  329. if (checksum != 0x7E)
  330. { printf ("Error 1 : %02X\n", checksum & 0xFF) ;
  331. }
  332. for (k = 2 ; k <= SDS_BLOCK_SIZE - 3 ; k ++)
  333. checksum ^= psds->read_data [k] ;
  334. checksum &= 0x7F ;
  335. if (checksum != psds->read_data [SDS_BLOCK_SIZE - 2])
  336. { psf_log_printf (psf, "Block %d : checksum is %02X should be %02X\n", psds->read_data [4], checksum, psds->read_data [SDS_BLOCK_SIZE - 2]) ;
  337. } ;
  338. ucptr = psds->read_data + 5 ;
  339. for (k = 0 ; k < 120 ; k += 2)
  340. { sample = (ucptr [k] << 25) + (ucptr [k + 1] << 18) ;
  341. psds->read_samples [k / 2] = (int) (sample - 0x80000000) ;
  342. } ;
  343. return 1 ;
  344. } /* sds_2byte_read */
  345. static int
  346. sds_3byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds)
  347. { unsigned char *ucptr, checksum ;
  348. unsigned int sample ;
  349. int k ;
  350. psds->read_block ++ ;
  351. psds->read_count = 0 ;
  352. if (psds->read_block * psds->samplesperblock > psds->frames)
  353. { memset (psds->read_samples, 0, psds->samplesperblock * sizeof (int)) ;
  354. return 1 ;
  355. } ;
  356. if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
  357. psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
  358. if (psds->read_data [0] != 0xF0)
  359. { printf ("Error A : %02X\n", psds->read_data [0] & 0xFF) ;
  360. } ;
  361. checksum = psds->read_data [1] ;
  362. if (checksum != 0x7E)
  363. { printf ("Error 1 : %02X\n", checksum & 0xFF) ;
  364. }
  365. for (k = 2 ; k <= SDS_BLOCK_SIZE - 3 ; k ++)
  366. checksum ^= psds->read_data [k] ;
  367. checksum &= 0x7F ;
  368. if (checksum != psds->read_data [SDS_BLOCK_SIZE - 2])
  369. { psf_log_printf (psf, "Block %d : checksum is %02X should be %02X\n", psds->read_data [4], checksum, psds->read_data [SDS_BLOCK_SIZE - 2]) ;
  370. } ;
  371. ucptr = psds->read_data + 5 ;
  372. for (k = 0 ; k < 120 ; k += 3)
  373. { sample = (ucptr [k] << 25) + (ucptr [k + 1] << 18) + (ucptr [k + 2] << 11) ;
  374. psds->read_samples [k / 3] = (int) (sample - 0x80000000) ;
  375. } ;
  376. return 1 ;
  377. } /* sds_3byte_read */
  378. static int
  379. sds_4byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds)
  380. { unsigned char *ucptr, checksum ;
  381. unsigned int sample ;
  382. int k ;
  383. psds->read_block ++ ;
  384. psds->read_count = 0 ;
  385. if (psds->read_block * psds->samplesperblock > psds->frames)
  386. { memset (psds->read_samples, 0, psds->samplesperblock * sizeof (int)) ;
  387. return 1 ;
  388. } ;
  389. if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
  390. psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
  391. if (psds->read_data [0] != 0xF0)
  392. { printf ("Error A : %02X\n", psds->read_data [0] & 0xFF) ;
  393. } ;
  394. checksum = psds->read_data [1] ;
  395. if (checksum != 0x7E)
  396. { printf ("Error 1 : %02X\n", checksum & 0xFF) ;
  397. }
  398. for (k = 2 ; k <= SDS_BLOCK_SIZE - 3 ; k ++)
  399. checksum ^= psds->read_data [k] ;
  400. checksum &= 0x7F ;
  401. if (checksum != psds->read_data [SDS_BLOCK_SIZE - 2])
  402. { psf_log_printf (psf, "Block %d : checksum is %02X should be %02X\n", psds->read_data [4], checksum, psds->read_data [SDS_BLOCK_SIZE - 2]) ;
  403. } ;
  404. ucptr = psds->read_data + 5 ;
  405. for (k = 0 ; k < 120 ; k += 4)
  406. { sample = (ucptr [k] << 25) + (ucptr [k + 1] << 18) + (ucptr [k + 2] << 11) + (ucptr [k + 3] << 4) ;
  407. psds->read_samples [k / 4] = (int) (sample - 0x80000000) ;
  408. } ;
  409. return 1 ;
  410. } /* sds_4byte_read */
  411. static sf_count_t
  412. sds_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
  413. { SDS_PRIVATE *psds ;
  414. int *iptr ;
  415. int k, bufferlen, readcount, count ;
  416. sf_count_t total = 0 ;
  417. if (psf->codec_data == NULL)
  418. return 0 ;
  419. psds = (SDS_PRIVATE*) psf->codec_data ;
  420. iptr = psf->u.ibuf ;
  421. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  422. while (len > 0)
  423. { readcount = (len >= bufferlen) ? bufferlen : len ;
  424. count = sds_read (psf, psds, iptr, readcount) ;
  425. for (k = 0 ; k < readcount ; k++)
  426. ptr [total + k] = iptr [k] >> 16 ;
  427. total += count ;
  428. len -= readcount ;
  429. } ;
  430. return total ;
  431. } /* sds_read_s */
  432. static sf_count_t
  433. sds_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
  434. { SDS_PRIVATE *psds ;
  435. int total ;
  436. if (psf->codec_data == NULL)
  437. return 0 ;
  438. psds = (SDS_PRIVATE*) psf->codec_data ;
  439. total = sds_read (psf, psds, ptr, len) ;
  440. return total ;
  441. } /* sds_read_i */
  442. static sf_count_t
  443. sds_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
  444. { SDS_PRIVATE *psds ;
  445. int *iptr ;
  446. int k, bufferlen, readcount, count ;
  447. sf_count_t total = 0 ;
  448. float normfact ;
  449. if (psf->codec_data == NULL)
  450. return 0 ;
  451. psds = (SDS_PRIVATE*) psf->codec_data ;
  452. if (psf->norm_float == SF_TRUE)
  453. normfact = 1.0 / 0x80000000 ;
  454. else
  455. normfact = 1.0 / (1 << psds->bitwidth) ;
  456. iptr = psf->u.ibuf ;
  457. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  458. while (len > 0)
  459. { readcount = (len >= bufferlen) ? bufferlen : len ;
  460. count = sds_read (psf, psds, iptr, readcount) ;
  461. for (k = 0 ; k < readcount ; k++)
  462. ptr [total + k] = normfact * iptr [k] ;
  463. total += count ;
  464. len -= readcount ;
  465. } ;
  466. return total ;
  467. } /* sds_read_f */
  468. static sf_count_t
  469. sds_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
  470. { SDS_PRIVATE *psds ;
  471. int *iptr ;
  472. int k, bufferlen, readcount, count ;
  473. sf_count_t total = 0 ;
  474. double normfact ;
  475. if (psf->codec_data == NULL)
  476. return 0 ;
  477. psds = (SDS_PRIVATE*) psf->codec_data ;
  478. if (psf->norm_double == SF_TRUE)
  479. normfact = 1.0 / 0x80000000 ;
  480. else
  481. normfact = 1.0 / (1 << psds->bitwidth) ;
  482. iptr = psf->u.ibuf ;
  483. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  484. while (len > 0)
  485. { readcount = (len >= bufferlen) ? bufferlen : len ;
  486. count = sds_read (psf, psds, iptr, readcount) ;
  487. for (k = 0 ; k < readcount ; k++)
  488. ptr [total + k] = normfact * iptr [k] ;
  489. total += count ;
  490. len -= readcount ;
  491. } ;
  492. return total ;
  493. } /* sds_read_d */
  494. static int
  495. sds_read (SF_PRIVATE *psf, SDS_PRIVATE *psds, int *ptr, int len)
  496. { int count, total = 0 ;
  497. while (total < len)
  498. { if (psds->read_block * psds->samplesperblock >= psds->frames)
  499. { memset (&(ptr [total]), 0, (len - total) * sizeof (int)) ;
  500. return total ;
  501. } ;
  502. if (psds->read_count >= psds->samplesperblock)
  503. psds->reader (psf, psds) ;
  504. count = (psds->samplesperblock - psds->read_count) ;
  505. count = (len - total > count) ? count : len - total ;
  506. memcpy (&(ptr [total]), &(psds->read_samples [psds->read_count]), count * sizeof (int)) ;
  507. total += count ;
  508. psds->read_count += count ;
  509. } ;
  510. return total ;
  511. } /* sds_read */
  512. /*==============================================================================
  513. */
  514. static sf_count_t
  515. sds_seek (SF_PRIVATE *psf, int mode, sf_count_t seek_from_start)
  516. { SDS_PRIVATE *psds ;
  517. sf_count_t file_offset ;
  518. int newblock, newsample ;
  519. if ((psds = psf->codec_data) == NULL)
  520. { psf->error = SFE_INTERNAL ;
  521. return PSF_SEEK_ERROR ;
  522. } ;
  523. if (psf->datalength < 0 || psf->dataoffset < 0)
  524. { psf->error = SFE_BAD_SEEK ;
  525. return PSF_SEEK_ERROR ;
  526. } ;
  527. if (seek_from_start < 0 || seek_from_start > psf->sf.frames)
  528. { psf->error = SFE_BAD_SEEK ;
  529. return PSF_SEEK_ERROR ;
  530. } ;
  531. if (mode == SFM_READ && psds->write_count > 0)
  532. psds->writer (psf, psds) ;
  533. newblock = seek_from_start / psds->samplesperblock ;
  534. newsample = seek_from_start % psds->samplesperblock ;
  535. switch (mode)
  536. { case SFM_READ :
  537. if (newblock > psds->total_blocks)
  538. { psf->error = SFE_BAD_SEEK ;
  539. return PSF_SEEK_ERROR ;
  540. } ;
  541. file_offset = psf->dataoffset + newblock * SDS_BLOCK_SIZE ;
  542. if (psf_fseek (psf, file_offset, SEEK_SET) != file_offset)
  543. { psf->error = SFE_SEEK_FAILED ;
  544. return PSF_SEEK_ERROR ;
  545. } ;
  546. psds->read_block = newblock ;
  547. psds->reader (psf, psds) ;
  548. psds->read_count = newsample ;
  549. break ;
  550. case SFM_WRITE :
  551. if (newblock > psds->total_blocks)
  552. { psf->error = SFE_BAD_SEEK ;
  553. return PSF_SEEK_ERROR ;
  554. } ;
  555. file_offset = psf->dataoffset + newblock * SDS_BLOCK_SIZE ;
  556. if (psf_fseek (psf, file_offset, SEEK_SET) != file_offset)
  557. { psf->error = SFE_SEEK_FAILED ;
  558. return PSF_SEEK_ERROR ;
  559. } ;
  560. psds->write_block = newblock ;
  561. psds->reader (psf, psds) ;
  562. psds->write_count = newsample ;
  563. break ;
  564. default :
  565. psf->error = SFE_BAD_SEEK ;
  566. return PSF_SEEK_ERROR ;
  567. break ;
  568. } ;
  569. return seek_from_start ;
  570. } /* sds_seek */
  571. /*==============================================================================
  572. */
  573. static int
  574. sds_2byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds)
  575. { unsigned char *ucptr, checksum ;
  576. unsigned int sample ;
  577. int k ;
  578. psds->write_data [0] = 0xF0 ;
  579. psds->write_data [1] = 0x7E ;
  580. psds->write_data [2] = 0 ; /* Channel number */
  581. psds->write_data [3] = 2 ;
  582. psds->write_data [4] = psds->write_block & 0x7F ; /* Packet number */
  583. ucptr = psds->write_data + 5 ;
  584. for (k = 0 ; k < 120 ; k += 2)
  585. { sample = psds->write_samples [k / 2] ;
  586. sample += 0x80000000 ;
  587. ucptr [k] = (sample >> 25) & 0x7F ;
  588. ucptr [k + 1] = (sample >> 18) & 0x7F ;
  589. } ;
  590. checksum = psds->write_data [1] ;
  591. for (k = 2 ; k <= SDS_BLOCK_SIZE - 3 ; k ++)
  592. checksum ^= psds->write_data [k] ;
  593. checksum &= 0x7F ;
  594. psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ;
  595. psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ;
  596. if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
  597. psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
  598. psds->write_block ++ ;
  599. psds->write_count = 0 ;
  600. if (psds->write_block > psds->total_blocks)
  601. psds->total_blocks = psds->write_block ;
  602. psds->frames = psds->total_blocks * psds->samplesperblock ;
  603. return 1 ;
  604. } /* sds_2byte_write */
  605. static int
  606. sds_3byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds)
  607. { unsigned char *ucptr, checksum ;
  608. unsigned int sample ;
  609. int k ;
  610. psds->write_data [0] = 0xF0 ;
  611. psds->write_data [1] = 0x7E ;
  612. psds->write_data [2] = 0 ; /* Channel number */
  613. psds->write_data [3] = 2 ;
  614. psds->write_data [4] = psds->write_block & 0x7F ; /* Packet number */
  615. ucptr = psds->write_data + 5 ;
  616. for (k = 0 ; k < 120 ; k += 3)
  617. { sample = psds->write_samples [k / 3] ;
  618. sample += 0x80000000 ;
  619. ucptr [k] = (sample >> 25) & 0x7F ;
  620. ucptr [k + 1] = (sample >> 18) & 0x7F ;
  621. ucptr [k + 2] = (sample >> 11) & 0x7F ;
  622. } ;
  623. checksum = psds->write_data [1] ;
  624. for (k = 2 ; k <= SDS_BLOCK_SIZE - 3 ; k ++)
  625. checksum ^= psds->write_data [k] ;
  626. checksum &= 0x7F ;
  627. psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ;
  628. psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ;
  629. if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
  630. psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
  631. psds->write_block ++ ;
  632. psds->write_count = 0 ;
  633. if (psds->write_block > psds->total_blocks)
  634. psds->total_blocks = psds->write_block ;
  635. psds->frames = psds->total_blocks * psds->samplesperblock ;
  636. return 1 ;
  637. } /* sds_3byte_write */
  638. static int
  639. sds_4byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds)
  640. { unsigned char *ucptr, checksum ;
  641. unsigned int sample ;
  642. int k ;
  643. psds->write_data [0] = 0xF0 ;
  644. psds->write_data [1] = 0x7E ;
  645. psds->write_data [2] = 0 ; /* Channel number */
  646. psds->write_data [3] = 2 ;
  647. psds->write_data [4] = psds->write_block & 0x7F ; /* Packet number */
  648. ucptr = psds->write_data + 5 ;
  649. for (k = 0 ; k < 120 ; k += 4)
  650. { sample = psds->write_samples [k / 4] ;
  651. sample += 0x80000000 ;
  652. ucptr [k] = (sample >> 25) & 0x7F ;
  653. ucptr [k + 1] = (sample >> 18) & 0x7F ;
  654. ucptr [k + 2] = (sample >> 11) & 0x7F ;
  655. ucptr [k + 3] = (sample >> 4) & 0x7F ;
  656. } ;
  657. checksum = psds->write_data [1] ;
  658. for (k = 2 ; k <= SDS_BLOCK_SIZE - 3 ; k ++)
  659. checksum ^= psds->write_data [k] ;
  660. checksum &= 0x7F ;
  661. psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ;
  662. psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ;
  663. if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
  664. psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
  665. psds->write_block ++ ;
  666. psds->write_count = 0 ;
  667. if (psds->write_block > psds->total_blocks)
  668. psds->total_blocks = psds->write_block ;
  669. psds->frames = psds->total_blocks * psds->samplesperblock ;
  670. return 1 ;
  671. } /* sds_4byte_write */
  672. static sf_count_t
  673. sds_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
  674. { SDS_PRIVATE *psds ;
  675. int *iptr ;
  676. int k, bufferlen, writecount, count ;
  677. sf_count_t total = 0 ;
  678. if (psf->codec_data == NULL)
  679. return 0 ;
  680. psds = (SDS_PRIVATE*) psf->codec_data ;
  681. psds->total_written += len ;
  682. iptr = psf->u.ibuf ;
  683. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  684. while (len > 0)
  685. { writecount = (len >= bufferlen) ? bufferlen : len ;
  686. for (k = 0 ; k < writecount ; k++)
  687. iptr [k] = ptr [total + k] << 16 ;
  688. count = sds_write (psf, psds, iptr, writecount) ;
  689. total += count ;
  690. len -= writecount ;
  691. } ;
  692. return total ;
  693. } /* sds_write_s */
  694. static sf_count_t
  695. sds_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
  696. { SDS_PRIVATE *psds ;
  697. int total ;
  698. if (psf->codec_data == NULL)
  699. return 0 ;
  700. psds = (SDS_PRIVATE*) psf->codec_data ;
  701. psds->total_written += len ;
  702. total = sds_write (psf, psds, ptr, len) ;
  703. return total ;
  704. } /* sds_write_i */
  705. static sf_count_t
  706. sds_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
  707. { SDS_PRIVATE *psds ;
  708. int *iptr ;
  709. int k, bufferlen, writecount, count ;
  710. sf_count_t total = 0 ;
  711. float normfact ;
  712. if (psf->codec_data == NULL)
  713. return 0 ;
  714. psds = (SDS_PRIVATE*) psf->codec_data ;
  715. psds->total_written += len ;
  716. if (psf->norm_float == SF_TRUE)
  717. normfact = 1.0 * 0x80000000 ;
  718. else
  719. normfact = 1.0 * (1 << psds->bitwidth) ;
  720. iptr = psf->u.ibuf ;
  721. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  722. while (len > 0)
  723. { writecount = (len >= bufferlen) ? bufferlen : len ;
  724. for (k = 0 ; k < writecount ; k++)
  725. iptr [k] = normfact * ptr [total + k] ;
  726. count = sds_write (psf, psds, iptr, writecount) ;
  727. total += count ;
  728. len -= writecount ;
  729. } ;
  730. return total ;
  731. } /* sds_write_f */
  732. static sf_count_t
  733. sds_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
  734. { SDS_PRIVATE *psds ;
  735. int *iptr ;
  736. int k, bufferlen, writecount, count ;
  737. sf_count_t total = 0 ;
  738. double normfact ;
  739. if (psf->codec_data == NULL)
  740. return 0 ;
  741. psds = (SDS_PRIVATE*) psf->codec_data ;
  742. psds->total_written += len ;
  743. if (psf->norm_double == SF_TRUE)
  744. normfact = 1.0 * 0x80000000 ;
  745. else
  746. normfact = 1.0 * (1 << psds->bitwidth) ;
  747. iptr = psf->u.ibuf ;
  748. bufferlen = ARRAY_LEN (psf->u.ibuf) ;
  749. while (len > 0)
  750. { writecount = (len >= bufferlen) ? bufferlen : len ;
  751. for (k = 0 ; k < writecount ; k++)
  752. iptr [k] = normfact * ptr [total + k] ;
  753. count = sds_write (psf, psds, iptr, writecount) ;
  754. total += count ;
  755. len -= writecount ;
  756. } ;
  757. return total ;
  758. } /* sds_write_d */
  759. static int
  760. sds_write (SF_PRIVATE *psf, SDS_PRIVATE *psds, const int *ptr, int len)
  761. { int count, total = 0 ;
  762. while (total < len)
  763. { count = psds->samplesperblock - psds->write_count ;
  764. if (count > len - total)
  765. count = len - total ;
  766. memcpy (&(psds->write_samples [psds->write_count]), &(ptr [total]), count * sizeof (int)) ;
  767. total += count ;
  768. psds->write_count += count ;
  769. if (psds->write_count >= psds->samplesperblock)
  770. psds->writer (psf, psds) ;
  771. } ;
  772. return total ;
  773. } /* sds_write */