paf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. ** Copyright (C) 1999-2017 Erik de Castro Lopo <[email protected]>
  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. ** Macros to handle big/little endian issues.
  30. */
  31. #define FAP_MARKER (MAKE_MARKER ('f', 'a', 'p', ' '))
  32. #define PAF_MARKER (MAKE_MARKER (' ', 'p', 'a', 'f'))
  33. /*------------------------------------------------------------------------------
  34. ** Other defines.
  35. */
  36. #define PAF_HEADER_LENGTH 2048
  37. #define PAF24_SAMPLES_PER_BLOCK 10
  38. #define PAF24_BLOCK_SIZE 32
  39. /*------------------------------------------------------------------------------
  40. ** Typedefs.
  41. */
  42. typedef struct
  43. { int version ;
  44. int endianness ;
  45. int samplerate ;
  46. int format ;
  47. int channels ;
  48. int source ;
  49. } PAF_FMT ;
  50. typedef struct
  51. { int max_blocks, channels, blocksize ;
  52. int read_block, write_block, read_count, write_count ;
  53. sf_count_t sample_count ;
  54. int *samples ;
  55. int *block ;
  56. int data [] ; /* ISO C99 struct flexible array. */
  57. } PAF24_PRIVATE ;
  58. /*------------------------------------------------------------------------------
  59. ** Private static functions.
  60. */
  61. static int paf24_init (SF_PRIVATE *psf) ;
  62. static int paf_read_header (SF_PRIVATE *psf) ;
  63. static int paf_write_header (SF_PRIVATE *psf, int calc_length) ;
  64. static sf_count_t paf24_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  65. static sf_count_t paf24_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  66. static sf_count_t paf24_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  67. static sf_count_t paf24_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  68. static sf_count_t paf24_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  69. static sf_count_t paf24_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  70. static sf_count_t paf24_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  71. static sf_count_t paf24_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
  72. static sf_count_t paf24_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
  73. enum
  74. { PAF_PCM_16 = 0,
  75. PAF_PCM_24 = 1,
  76. PAF_PCM_S8 = 2
  77. } ;
  78. /*------------------------------------------------------------------------------
  79. ** Public function.
  80. */
  81. int
  82. paf_open (SF_PRIVATE *psf)
  83. { int subformat, error, endian ;
  84. psf->dataoffset = PAF_HEADER_LENGTH ;
  85. if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
  86. { if ((error = paf_read_header (psf)))
  87. return error ;
  88. } ;
  89. subformat = SF_CODEC (psf->sf.format) ;
  90. if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
  91. { if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_PAF)
  92. return SFE_BAD_OPEN_FORMAT ;
  93. endian = SF_ENDIAN (psf->sf.format) ;
  94. /* PAF is by default big endian. */
  95. psf->endian = SF_ENDIAN_BIG ;
  96. if (endian == SF_ENDIAN_LITTLE || (CPU_IS_LITTLE_ENDIAN && (endian == SF_ENDIAN_CPU)))
  97. psf->endian = SF_ENDIAN_LITTLE ;
  98. if ((error = paf_write_header (psf, SF_FALSE)))
  99. return error ;
  100. psf->write_header = paf_write_header ;
  101. } ;
  102. switch (subformat)
  103. { case SF_FORMAT_PCM_S8 :
  104. psf->bytewidth = 1 ;
  105. error = pcm_init (psf) ;
  106. break ;
  107. case SF_FORMAT_PCM_16 :
  108. psf->bytewidth = 2 ;
  109. error = pcm_init (psf) ;
  110. break ;
  111. case SF_FORMAT_PCM_24 :
  112. /* No bytewidth because of whacky 24 bit encoding. */
  113. error = paf24_init (psf) ;
  114. break ;
  115. default : return SFE_PAF_UNKNOWN_FORMAT ;
  116. } ;
  117. return error ;
  118. } /* paf_open */
  119. /*------------------------------------------------------------------------------
  120. */
  121. static int
  122. paf_read_header (SF_PRIVATE *psf)
  123. { PAF_FMT paf_fmt ;
  124. int marker ;
  125. if (psf->filelength < PAF_HEADER_LENGTH)
  126. return SFE_PAF_SHORT_HEADER ;
  127. memset (&paf_fmt, 0, sizeof (paf_fmt)) ;
  128. psf_binheader_readf (psf, "pm", 0, &marker) ;
  129. psf_log_printf (psf, "Signature : '%M'\n", marker) ;
  130. if (marker == PAF_MARKER)
  131. { psf_binheader_readf (psf, "E444444", &(paf_fmt.version), &(paf_fmt.endianness),
  132. &(paf_fmt.samplerate), &(paf_fmt.format), &(paf_fmt.channels), &(paf_fmt.source)) ;
  133. }
  134. else if (marker == FAP_MARKER)
  135. { psf_binheader_readf (psf, "e444444", &(paf_fmt.version), &(paf_fmt.endianness),
  136. &(paf_fmt.samplerate), &(paf_fmt.format), &(paf_fmt.channels), &(paf_fmt.source)) ;
  137. }
  138. else
  139. return SFE_PAF_NO_MARKER ;
  140. psf_log_printf (psf, "Version : %d\n", paf_fmt.version) ;
  141. if (paf_fmt.version != 0)
  142. { psf_log_printf (psf, "*** Bad version number. should be zero.\n") ;
  143. return SFE_PAF_VERSION ;
  144. } ;
  145. psf_log_printf (psf, "Sample Rate : %d\n", paf_fmt.samplerate) ;
  146. psf_log_printf (psf, "Channels : %d\n", paf_fmt.channels) ;
  147. psf_log_printf (psf, "Endianness : %d => ", paf_fmt.endianness) ;
  148. if (paf_fmt.endianness)
  149. { psf_log_printf (psf, "Little\n", paf_fmt.endianness) ;
  150. psf->endian = SF_ENDIAN_LITTLE ;
  151. }
  152. else
  153. { psf_log_printf (psf, "Big\n", paf_fmt.endianness) ;
  154. psf->endian = SF_ENDIAN_BIG ;
  155. } ;
  156. if (paf_fmt.channels < 1 || paf_fmt.channels > SF_MAX_CHANNELS)
  157. return SFE_PAF_BAD_CHANNELS ;
  158. psf->datalength = psf->filelength - psf->dataoffset ;
  159. psf_binheader_readf (psf, "p", (int) psf->dataoffset) ;
  160. psf->sf.samplerate = paf_fmt.samplerate ;
  161. psf->sf.channels = paf_fmt.channels ;
  162. /* Only fill in type major. */
  163. psf->sf.format = SF_FORMAT_PAF ;
  164. psf_log_printf (psf, "Format : %d => ", paf_fmt.format) ;
  165. /* PAF is by default big endian. */
  166. psf->sf.format |= paf_fmt.endianness ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG ;
  167. switch (paf_fmt.format)
  168. { case PAF_PCM_S8 :
  169. psf_log_printf (psf, "8 bit linear PCM\n") ;
  170. psf->bytewidth = 1 ;
  171. psf->sf.format |= SF_FORMAT_PCM_S8 ;
  172. psf->blockwidth = psf->bytewidth * psf->sf.channels ;
  173. psf->sf.frames = psf->datalength / psf->blockwidth ;
  174. break ;
  175. case PAF_PCM_16 :
  176. psf_log_printf (psf, "16 bit linear PCM\n") ;
  177. psf->bytewidth = 2 ;
  178. psf->sf.format |= SF_FORMAT_PCM_16 ;
  179. psf->blockwidth = psf->bytewidth * psf->sf.channels ;
  180. psf->sf.frames = psf->datalength / psf->blockwidth ;
  181. break ;
  182. case PAF_PCM_24 :
  183. psf_log_printf (psf, "24 bit linear PCM\n") ;
  184. psf->bytewidth = 3 ;
  185. psf->sf.format |= SF_FORMAT_PCM_24 ;
  186. psf->blockwidth = 0 ;
  187. psf->sf.frames = PAF24_SAMPLES_PER_BLOCK * psf->datalength /
  188. (PAF24_BLOCK_SIZE * psf->sf.channels) ;
  189. break ;
  190. default : psf_log_printf (psf, "Unknown\n") ;
  191. return SFE_PAF_UNKNOWN_FORMAT ;
  192. break ;
  193. } ;
  194. psf_log_printf (psf, "Source : %d => ", paf_fmt.source) ;
  195. switch (paf_fmt.source)
  196. { case 1 : psf_log_printf (psf, "Analog Recording\n") ;
  197. break ;
  198. case 2 : psf_log_printf (psf, "Digital Transfer\n") ;
  199. break ;
  200. case 3 : psf_log_printf (psf, "Multi-track Mixdown\n") ;
  201. break ;
  202. case 5 : psf_log_printf (psf, "Audio Resulting From DSP Processing\n") ;
  203. break ;
  204. default : psf_log_printf (psf, "Unknown\n") ;
  205. break ;
  206. } ;
  207. return 0 ;
  208. } /* paf_read_header */
  209. static int
  210. paf_write_header (SF_PRIVATE *psf, int UNUSED (calc_length))
  211. { int paf_format ;
  212. /* PAF header already written so no need to re-write. */
  213. if (psf_ftell (psf) >= PAF_HEADER_LENGTH)
  214. return 0 ;
  215. psf->dataoffset = PAF_HEADER_LENGTH ;
  216. switch (SF_CODEC (psf->sf.format))
  217. { case SF_FORMAT_PCM_S8 :
  218. paf_format = PAF_PCM_S8 ;
  219. break ;
  220. case SF_FORMAT_PCM_16 :
  221. paf_format = PAF_PCM_16 ;
  222. break ;
  223. case SF_FORMAT_PCM_24 :
  224. paf_format = PAF_PCM_24 ;
  225. break ;
  226. default : return SFE_PAF_UNKNOWN_FORMAT ;
  227. } ;
  228. /* Reset the current header length to zero. */
  229. psf->header.ptr [0] = 0 ;
  230. psf->header.indx = 0 ;
  231. if (psf->endian == SF_ENDIAN_BIG)
  232. { /* Marker, version, endianness, samplerate */
  233. psf_binheader_writef (psf, "Em444", BHWm (PAF_MARKER), BHW4 (0), BHW4 (0), BHW4 (psf->sf.samplerate)) ;
  234. /* format, channels, source */
  235. psf_binheader_writef (psf, "E444", BHW4 (paf_format), BHW4 (psf->sf.channels), BHW4 (0)) ;
  236. }
  237. else if (psf->endian == SF_ENDIAN_LITTLE)
  238. { /* Marker, version, endianness, samplerate */
  239. psf_binheader_writef (psf, "em444", BHWm (FAP_MARKER), BHW4 (0), BHW4 (1), BHW4 (psf->sf.samplerate)) ;
  240. /* format, channels, source */
  241. psf_binheader_writef (psf, "e444", BHW4 (paf_format), BHW4 (psf->sf.channels), BHW4 (0)) ;
  242. } ;
  243. /* Zero fill to dataoffset. */
  244. psf_binheader_writef (psf, "z", BHWz ((size_t) (psf->dataoffset - psf->header.indx))) ;
  245. psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
  246. return psf->error ;
  247. } /* paf_write_header */
  248. /*===============================================================================
  249. ** 24 bit PAF files have a really weird encoding.
  250. ** For a mono file, 10 samples (each being 3 bytes) are packed into a 32 byte
  251. ** block. The 8 ints in this 32 byte block are then endian swapped (as ints)
  252. ** if necessary before being written to disk.
  253. ** For a stereo file, blocks of 10 samples from the same channel are encoded
  254. ** into 32 bytes as for the mono case. The 32 byte blocks are then interleaved
  255. ** on disk.
  256. ** Reading has to reverse the above process :-).
  257. ** Weird!!!
  258. **
  259. ** The code below attempts to gain efficiency while maintaining readability.
  260. */
  261. static int paf24_read_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24) ;
  262. static int paf24_write_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24) ;
  263. static int paf24_close (SF_PRIVATE *psf) ;
  264. static int
  265. paf24_init (SF_PRIVATE *psf)
  266. { PAF24_PRIVATE *ppaf24 ;
  267. int paf24size ;
  268. paf24size = sizeof (PAF24_PRIVATE) + psf->sf.channels *
  269. (PAF24_BLOCK_SIZE + PAF24_SAMPLES_PER_BLOCK * sizeof (int)) ;
  270. /*
  271. ** Not exatly sure why this needs to be here but the tests
  272. ** fail without it.
  273. */
  274. psf->last_op = 0 ;
  275. if (! (psf->codec_data = calloc (1, paf24size)))
  276. return SFE_MALLOC_FAILED ;
  277. ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
  278. ppaf24->channels = psf->sf.channels ;
  279. ppaf24->samples = ppaf24->data ;
  280. ppaf24->block = ppaf24->data + PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ;
  281. ppaf24->blocksize = PAF24_BLOCK_SIZE * ppaf24->channels ;
  282. if (psf->file.mode == SFM_READ || psf->file.mode == SFM_RDWR)
  283. { paf24_read_block (psf, ppaf24) ; /* Read first block. */
  284. psf->read_short = paf24_read_s ;
  285. psf->read_int = paf24_read_i ;
  286. psf->read_float = paf24_read_f ;
  287. psf->read_double = paf24_read_d ;
  288. } ;
  289. if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
  290. { psf->write_short = paf24_write_s ;
  291. psf->write_int = paf24_write_i ;
  292. psf->write_float = paf24_write_f ;
  293. psf->write_double = paf24_write_d ;
  294. } ;
  295. psf->seek = paf24_seek ;
  296. psf->container_close = paf24_close ;
  297. psf->filelength = psf_get_filelen (psf) ;
  298. psf->datalength = psf->filelength - psf->dataoffset ;
  299. if (psf->datalength % PAF24_BLOCK_SIZE)
  300. { if (psf->file.mode == SFM_READ)
  301. psf_log_printf (psf, "*** Warning : file seems to be truncated.\n") ;
  302. ppaf24->max_blocks = psf->datalength / ppaf24->blocksize + 1 ;
  303. }
  304. else
  305. ppaf24->max_blocks = psf->datalength / ppaf24->blocksize ;
  306. ppaf24->read_block = 0 ;
  307. if (psf->file.mode == SFM_RDWR)
  308. ppaf24->write_block = ppaf24->max_blocks ;
  309. else
  310. ppaf24->write_block = 0 ;
  311. psf->sf.frames = PAF24_SAMPLES_PER_BLOCK * ppaf24->max_blocks ;
  312. ppaf24->sample_count = psf->sf.frames ;
  313. return 0 ;
  314. } /* paf24_init */
  315. static sf_count_t
  316. paf24_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
  317. { PAF24_PRIVATE *ppaf24 ;
  318. int newblock, newsample ;
  319. if (psf->codec_data == NULL)
  320. { psf->error = SFE_INTERNAL ;
  321. return PSF_SEEK_ERROR ;
  322. } ;
  323. ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
  324. if (mode == SFM_READ && ppaf24->write_count > 0)
  325. paf24_write_block (psf, ppaf24) ;
  326. newblock = offset / PAF24_SAMPLES_PER_BLOCK ;
  327. newsample = offset % PAF24_SAMPLES_PER_BLOCK ;
  328. switch (mode)
  329. { case SFM_READ :
  330. if (psf->last_op == SFM_WRITE && ppaf24->write_count)
  331. paf24_write_block (psf, ppaf24) ;
  332. psf_fseek (psf, psf->dataoffset + newblock * ppaf24->blocksize, SEEK_SET) ;
  333. ppaf24->read_block = newblock ;
  334. paf24_read_block (psf, ppaf24) ;
  335. ppaf24->read_count = newsample ;
  336. break ;
  337. case SFM_WRITE :
  338. if (offset > ppaf24->sample_count)
  339. { psf->error = SFE_BAD_SEEK ;
  340. return PSF_SEEK_ERROR ;
  341. } ;
  342. if (psf->last_op == SFM_WRITE && ppaf24->write_count)
  343. paf24_write_block (psf, ppaf24) ;
  344. psf_fseek (psf, psf->dataoffset + newblock * ppaf24->blocksize, SEEK_SET) ;
  345. ppaf24->write_block = newblock ;
  346. paf24_read_block (psf, ppaf24) ;
  347. ppaf24->write_count = newsample ;
  348. break ;
  349. default :
  350. psf->error = SFE_BAD_SEEK ;
  351. return PSF_SEEK_ERROR ;
  352. } ;
  353. return newblock * PAF24_SAMPLES_PER_BLOCK + newsample ;
  354. } /* paf24_seek */
  355. static int
  356. paf24_close (SF_PRIVATE *psf)
  357. { PAF24_PRIVATE *ppaf24 ;
  358. if (psf->codec_data == NULL)
  359. return 0 ;
  360. ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
  361. if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
  362. { if (ppaf24->write_count > 0)
  363. paf24_write_block (psf, ppaf24) ;
  364. } ;
  365. return 0 ;
  366. } /* paf24_close */
  367. /*---------------------------------------------------------------------------
  368. */
  369. static int
  370. paf24_read_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24)
  371. { int k, channel ;
  372. unsigned char *cptr ;
  373. ppaf24->read_block ++ ;
  374. ppaf24->read_count = 0 ;
  375. if (ppaf24->read_block * PAF24_SAMPLES_PER_BLOCK > ppaf24->sample_count)
  376. { memset (ppaf24->samples, 0, PAF24_SAMPLES_PER_BLOCK * ppaf24->channels) ;
  377. return 1 ;
  378. } ;
  379. /* Read the block. */
  380. if ((k = (int) psf_fread (ppaf24->block, 1, ppaf24->blocksize, psf)) != ppaf24->blocksize)
  381. psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, ppaf24->blocksize) ;
  382. /* Do endian swapping if necessary. */
  383. if ((CPU_IS_BIG_ENDIAN && psf->endian == SF_ENDIAN_LITTLE) || (CPU_IS_LITTLE_ENDIAN && psf->endian == SF_ENDIAN_BIG))
  384. endswap_int_array (ppaf24->block, 8 * ppaf24->channels) ;
  385. /* Unpack block. */
  386. for (k = 0 ; k < PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ; k++)
  387. { channel = k % ppaf24->channels ;
  388. cptr = ((unsigned char *) ppaf24->block) + PAF24_BLOCK_SIZE * channel + 3 * (k / ppaf24->channels) ;
  389. ppaf24->samples [k] = (cptr [0] << 8) | (cptr [1] << 16) | (((unsigned) cptr [2]) << 24) ;
  390. } ;
  391. return 1 ;
  392. } /* paf24_read_block */
  393. static int
  394. paf24_read (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24, int *ptr, int len)
  395. { int count, total = 0 ;
  396. while (total < len)
  397. { if (ppaf24->read_block * PAF24_SAMPLES_PER_BLOCK >= ppaf24->sample_count)
  398. { memset (&(ptr [total]), 0, (len - total) * sizeof (int)) ;
  399. return total ;
  400. } ;
  401. if (ppaf24->read_count >= PAF24_SAMPLES_PER_BLOCK)
  402. paf24_read_block (psf, ppaf24) ;
  403. count = (PAF24_SAMPLES_PER_BLOCK - ppaf24->read_count) * ppaf24->channels ;
  404. count = (len - total > count) ? count : len - total ;
  405. memcpy (&(ptr [total]), &(ppaf24->samples [ppaf24->read_count * ppaf24->channels]), count * sizeof (int)) ;
  406. total += count ;
  407. ppaf24->read_count += count / ppaf24->channels ;
  408. } ;
  409. return total ;
  410. } /* paf24_read */
  411. static sf_count_t
  412. paf24_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
  413. { BUF_UNION ubuf ;
  414. PAF24_PRIVATE *ppaf24 ;
  415. int *iptr ;
  416. int k, bufferlen, readcount, count ;
  417. sf_count_t total = 0 ;
  418. if (psf->codec_data == NULL)
  419. return 0 ;
  420. ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
  421. iptr = ubuf.ibuf ;
  422. bufferlen = ARRAY_LEN (ubuf.ibuf) ;
  423. while (len > 0)
  424. { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
  425. count = paf24_read (psf, ppaf24, iptr, readcount) ;
  426. for (k = 0 ; k < readcount ; k++)
  427. ptr [total + k] = iptr [k] >> 16 ;
  428. total += count ;
  429. len -= readcount ;
  430. } ;
  431. return total ;
  432. } /* paf24_read_s */
  433. static sf_count_t
  434. paf24_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
  435. { PAF24_PRIVATE *ppaf24 ;
  436. int total ;
  437. if (psf->codec_data == NULL)
  438. return 0 ;
  439. ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
  440. total = paf24_read (psf, ppaf24, ptr, len) ;
  441. return total ;
  442. } /* paf24_read_i */
  443. static sf_count_t
  444. paf24_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
  445. { BUF_UNION ubuf ;
  446. PAF24_PRIVATE *ppaf24 ;
  447. int *iptr ;
  448. int k, bufferlen, readcount, count ;
  449. sf_count_t total = 0 ;
  450. float normfact ;
  451. if (psf->codec_data == NULL)
  452. return 0 ;
  453. ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
  454. normfact = (psf->norm_float == SF_TRUE) ? (1.0 / 0x80000000) : (1.0 / 0x100) ;
  455. iptr = ubuf.ibuf ;
  456. bufferlen = ARRAY_LEN (ubuf.ibuf) ;
  457. while (len > 0)
  458. { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
  459. count = paf24_read (psf, ppaf24, iptr, readcount) ;
  460. for (k = 0 ; k < readcount ; k++)
  461. ptr [total + k] = normfact * iptr [k] ;
  462. total += count ;
  463. len -= readcount ;
  464. } ;
  465. return total ;
  466. } /* paf24_read_f */
  467. static sf_count_t
  468. paf24_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
  469. { BUF_UNION ubuf ;
  470. PAF24_PRIVATE *ppaf24 ;
  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. ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
  478. normfact = (psf->norm_double == SF_TRUE) ? (1.0 / 0x80000000) : (1.0 / 0x100) ;
  479. iptr = ubuf.ibuf ;
  480. bufferlen = ARRAY_LEN (ubuf.ibuf) ;
  481. while (len > 0)
  482. { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
  483. count = paf24_read (psf, ppaf24, iptr, readcount) ;
  484. for (k = 0 ; k < readcount ; k++)
  485. ptr [total + k] = normfact * iptr [k] ;
  486. total += count ;
  487. len -= readcount ;
  488. } ;
  489. return total ;
  490. } /* paf24_read_d */
  491. /*---------------------------------------------------------------------------
  492. */
  493. static int
  494. paf24_write_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24)
  495. { int k, nextsample, channel ;
  496. unsigned char *cptr ;
  497. /* First pack block. */
  498. if (CPU_IS_LITTLE_ENDIAN)
  499. { for (k = 0 ; k < PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ; k++)
  500. { channel = k % ppaf24->channels ;
  501. cptr = ((unsigned char *) ppaf24->block) + PAF24_BLOCK_SIZE * channel + 3 * (k / ppaf24->channels) ;
  502. nextsample = ppaf24->samples [k] >> 8 ;
  503. cptr [0] = nextsample ;
  504. cptr [1] = nextsample >> 8 ;
  505. cptr [2] = nextsample >> 16 ;
  506. } ;
  507. /* Do endian swapping if necessary. */
  508. if (psf->endian == SF_ENDIAN_BIG)
  509. endswap_int_array (ppaf24->block, 8 * ppaf24->channels) ;
  510. }
  511. else if (CPU_IS_BIG_ENDIAN)
  512. { /* This is correct. */
  513. for (k = 0 ; k < PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ; k++)
  514. { channel = k % ppaf24->channels ;
  515. cptr = ((unsigned char *) ppaf24->block) + PAF24_BLOCK_SIZE * channel + 3 * (k / ppaf24->channels) ;
  516. nextsample = ppaf24->samples [k] >> 8 ;
  517. cptr [0] = nextsample ;
  518. cptr [1] = nextsample >> 8 ;
  519. cptr [2] = nextsample >> 16 ;
  520. } ;
  521. if (psf->endian == SF_ENDIAN_LITTLE)
  522. endswap_int_array (ppaf24->block, 8 * ppaf24->channels) ;
  523. } ;
  524. /* Write block to disk. */
  525. if ((k = (int) psf_fwrite (ppaf24->block, 1, ppaf24->blocksize, psf)) != ppaf24->blocksize)
  526. psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, ppaf24->blocksize) ;
  527. if (ppaf24->sample_count < ppaf24->write_block * PAF24_SAMPLES_PER_BLOCK + ppaf24->write_count)
  528. ppaf24->sample_count = ppaf24->write_block * PAF24_SAMPLES_PER_BLOCK + ppaf24->write_count ;
  529. if (ppaf24->write_count == PAF24_SAMPLES_PER_BLOCK)
  530. { ppaf24->write_block ++ ;
  531. ppaf24->write_count = 0 ;
  532. } ;
  533. return 1 ;
  534. } /* paf24_write_block */
  535. static int
  536. paf24_write (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24, const int *ptr, int len)
  537. { int count, total = 0 ;
  538. while (total < len)
  539. { count = (PAF24_SAMPLES_PER_BLOCK - ppaf24->write_count) * ppaf24->channels ;
  540. if (count > len - total)
  541. count = len - total ;
  542. memcpy (&(ppaf24->samples [ppaf24->write_count * ppaf24->channels]), &(ptr [total]), count * sizeof (int)) ;
  543. total += count ;
  544. ppaf24->write_count += count / ppaf24->channels ;
  545. if (ppaf24->write_count >= PAF24_SAMPLES_PER_BLOCK)
  546. paf24_write_block (psf, ppaf24) ;
  547. } ;
  548. return total ;
  549. } /* paf24_write */
  550. static sf_count_t
  551. paf24_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
  552. { BUF_UNION ubuf ;
  553. PAF24_PRIVATE *ppaf24 ;
  554. int *iptr ;
  555. int k, bufferlen, writecount = 0, count ;
  556. sf_count_t total = 0 ;
  557. if (psf->codec_data == NULL)
  558. return 0 ;
  559. ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
  560. iptr = ubuf.ibuf ;
  561. bufferlen = ARRAY_LEN (ubuf.ibuf) ;
  562. while (len > 0)
  563. { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
  564. for (k = 0 ; k < writecount ; k++)
  565. iptr [k] = ptr [total + k] << 16 ;
  566. count = paf24_write (psf, ppaf24, iptr, writecount) ;
  567. total += count ;
  568. len -= writecount ;
  569. if (count != writecount)
  570. break ;
  571. } ;
  572. return total ;
  573. } /* paf24_write_s */
  574. static sf_count_t
  575. paf24_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
  576. { PAF24_PRIVATE *ppaf24 ;
  577. int writecount, count ;
  578. sf_count_t total = 0 ;
  579. if (psf->codec_data == NULL)
  580. return 0 ;
  581. ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
  582. while (len > 0)
  583. { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
  584. count = paf24_write (psf, ppaf24, ptr, writecount) ;
  585. total += count ;
  586. len -= count ;
  587. if (count != writecount)
  588. break ;
  589. } ;
  590. return total ;
  591. } /* paf24_write_i */
  592. static sf_count_t
  593. paf24_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
  594. { BUF_UNION ubuf ;
  595. PAF24_PRIVATE *ppaf24 ;
  596. int *iptr ;
  597. int k, bufferlen, writecount = 0, count ;
  598. sf_count_t total = 0 ;
  599. float normfact ;
  600. if (psf->codec_data == NULL)
  601. return 0 ;
  602. ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
  603. normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : (1.0 / 0x100) ;
  604. iptr = ubuf.ibuf ;
  605. bufferlen = ARRAY_LEN (ubuf.ibuf) ;
  606. while (len > 0)
  607. { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
  608. for (k = 0 ; k < writecount ; k++)
  609. iptr [k] = psf_lrintf (normfact * ptr [total + k]) ;
  610. count = paf24_write (psf, ppaf24, iptr, writecount) ;
  611. total += count ;
  612. len -= writecount ;
  613. if (count != writecount)
  614. break ;
  615. } ;
  616. return total ;
  617. } /* paf24_write_f */
  618. static sf_count_t
  619. paf24_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
  620. { BUF_UNION ubuf ;
  621. PAF24_PRIVATE *ppaf24 ;
  622. int *iptr ;
  623. int k, bufferlen, writecount = 0, count ;
  624. sf_count_t total = 0 ;
  625. double normfact ;
  626. if (psf->codec_data == NULL)
  627. return 0 ;
  628. ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
  629. normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : (1.0 / 0x100) ;
  630. iptr = ubuf.ibuf ;
  631. bufferlen = ARRAY_LEN (ubuf.ibuf) ;
  632. while (len > 0)
  633. { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
  634. for (k = 0 ; k < writecount ; k++)
  635. iptr [k] = psf_lrint (normfact * ptr [total+k]) ;
  636. count = paf24_write (psf, ppaf24, iptr, writecount) ;
  637. total += count ;
  638. len -= writecount ;
  639. if (count != writecount)
  640. break ;
  641. } ;
  642. return total ;
  643. } /* paf24_write_d */