sndfile-salvage.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. ** Copyright (C) 2010-2014 Erik de Castro Lopo <[email protected]>
  3. **
  4. ** All rights reserved.
  5. **
  6. ** Redistribution and use in source and binary forms, with or without
  7. ** modification, are permitted provided that the following conditions are
  8. ** met:
  9. **
  10. ** * Redistributions of source code must retain the above copyright
  11. ** notice, this list of conditions and the following disclaimer.
  12. ** * Redistributions in binary form must reproduce the above copyright
  13. ** notice, this list of conditions and the following disclaimer in
  14. ** the documentation and/or other materials provided with the
  15. ** distribution.
  16. ** * Neither the author nor the names of any contributors may be used
  17. ** to endorse or promote products derived from this software without
  18. ** specific prior written permission.
  19. **
  20. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "sfconfig.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <inttypes.h>
  37. #include <ctype.h>
  38. #include <math.h>
  39. #include <errno.h>
  40. #if HAVE_UNISTD_H
  41. #include <unistd.h>
  42. #else
  43. #include "sf_unistd.h"
  44. #endif
  45. #include <fcntl.h>
  46. #include <sys/stat.h>
  47. #include <sys/types.h>
  48. #include <sndfile.h>
  49. #include "common.h"
  50. #define BUFFER_LEN (1 << 16)
  51. #define NOT(x) (! (x))
  52. #ifndef _WIN32
  53. typedef off_t sf_off_t ;
  54. #else
  55. typedef long long sf_off_t ;
  56. #endif
  57. static void usage_exit (const char *progname) ;
  58. static void salvage_file (const char * broken_wav, const char * fixed_w64) ;
  59. int
  60. main (int argc, char *argv [])
  61. {
  62. if (argc != 3)
  63. usage_exit (program_name (argv [0])) ;
  64. salvage_file (argv [1], argv [2]) ;
  65. return 0 ;
  66. } /* main */
  67. /*==============================================================================
  68. */
  69. static void lseek_or_die (int fd, sf_off_t offset, int whence) ;
  70. static sf_off_t get_file_length (int fd, const char * name) ;
  71. static sf_count_t find_data_offset (int fd, int format) ;
  72. static void copy_data (int fd, SNDFILE * sndfile, int readsize) ;
  73. static void
  74. usage_exit (const char *progname)
  75. { printf ("Usage :\n\n %s <broken wav file> <fixed w64 file>\n\n", progname) ;
  76. puts ("Salvages the audio data from WAV files which are more than 4G in length.\n") ;
  77. printf ("Using %s.\n\n", sf_version_string ()) ;
  78. exit (1) ;
  79. } /* usage_exit */
  80. static void
  81. salvage_file (const char * broken_wav, const char * fixed_w64)
  82. { SNDFILE * sndfile ;
  83. SF_INFO sfinfo ;
  84. sf_count_t broken_len, data_offset ;
  85. int fd, read_size ;
  86. if (strcmp (broken_wav, fixed_w64) == 0)
  87. { printf ("Error : Input and output files must be different.\n\n") ;
  88. exit (1) ;
  89. } ;
  90. if ((fd = open (broken_wav, O_RDONLY)) < 0)
  91. { printf ("Error : Not able to open file '%s' : %s\n", broken_wav, strerror (errno)) ;
  92. exit (1) ;
  93. } ;
  94. broken_len = get_file_length (fd, broken_wav) ;
  95. if (broken_len <= 0xffffffff)
  96. printf ("File is not greater than 4Gig but salvaging anyway.\n") ;
  97. /* Grab the format info from the broken file. */
  98. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  99. if ((sndfile = sf_open (broken_wav, SFM_READ, &sfinfo)) == NULL)
  100. { printf ("sf_open ('%s') failed : %s\n", broken_wav, sf_strerror (NULL)) ;
  101. exit (1) ;
  102. } ;
  103. sf_close (sndfile) ;
  104. data_offset = find_data_offset (fd, sfinfo.format & SF_FORMAT_TYPEMASK) ;
  105. printf ("Offset to audio data : %" PRId64 "\n", data_offset) ;
  106. switch (sfinfo.format & SF_FORMAT_TYPEMASK)
  107. { case SF_FORMAT_WAV :
  108. case SF_FORMAT_WAVEX :
  109. sfinfo.format = SF_FORMAT_W64 | (sfinfo.format & SF_FORMAT_SUBMASK) ;
  110. break ;
  111. default :
  112. printf ("Don't currently support this file type.\n") ;
  113. exit (1) ;
  114. } ;
  115. switch (sfinfo.format & SF_FORMAT_SUBMASK)
  116. { case SF_FORMAT_PCM_U8 :
  117. case SF_FORMAT_PCM_S8 :
  118. read_size = 1 ;
  119. break ;
  120. case SF_FORMAT_PCM_16 :
  121. read_size = 2 ;
  122. break ;
  123. case SF_FORMAT_PCM_24 :
  124. read_size = 3 ;
  125. break ;
  126. case SF_FORMAT_PCM_32 :
  127. case SF_FORMAT_FLOAT :
  128. read_size = 4 ;
  129. break ;
  130. case SF_FORMAT_DOUBLE :
  131. read_size = 8 ;
  132. break ;
  133. default :
  134. printf ("Sorry, don't currently support this file encoding type.\n") ;
  135. exit (1) ;
  136. } ;
  137. read_size *= sfinfo.channels ;
  138. if ((sndfile = sf_open (fixed_w64, SFM_WRITE, &sfinfo)) == NULL)
  139. { printf ("sf_open ('%s') failed : %s\n", fixed_w64, sf_strerror (NULL)) ;
  140. exit (1) ;
  141. } ;
  142. lseek_or_die (fd, data_offset, SEEK_SET) ;
  143. copy_data (fd, sndfile, read_size) ;
  144. sf_close (sndfile) ;
  145. puts ("Done!") ;
  146. } /* salvage_file */
  147. /*------------------------------------------------------------------------------
  148. */
  149. static void
  150. lseek_or_die (int fd, sf_off_t offset, int whence)
  151. {
  152. #ifndef _WIN32
  153. if (lseek (fd, offset, whence) < 0)
  154. #else
  155. if (_lseeki64 (fd, offset, whence) < 0)
  156. #endif
  157. { printf ("lseek failed : %s\n", strerror (errno)) ;
  158. exit (1) ;
  159. } ;
  160. return ;
  161. } /* lseek_or_die */
  162. static sf_off_t
  163. get_file_length (int fd, const char * name)
  164. {
  165. #ifndef _WIN32
  166. struct stat sbuf ;
  167. #else
  168. struct _stat64 sbuf ;
  169. #endif
  170. if (sizeof (sbuf.st_size) != 8)
  171. { puts ("Error : sizeof (sbuf.st_size) != 8. Was program compiled with\n"
  172. " 64 bit file offsets?\n") ;
  173. exit (1) ;
  174. } ;
  175. #ifndef _WIN32
  176. if (fstat (fd, &sbuf) != 0)
  177. #else
  178. if (_fstat64 (fd, &sbuf) != 0)
  179. #endif
  180. { printf ("Error : fstat ('%s') failed : %s\n", name, strerror (errno)) ;
  181. exit (1) ;
  182. } ;
  183. return sbuf.st_size ;
  184. } /* get_file_length */
  185. static sf_count_t
  186. find_data_offset (int fd, int format)
  187. { char buffer [8192], *cptr ;
  188. const char * target = "XXXX" ;
  189. sf_count_t offset = -1, extra ;
  190. int rlen, slen ;
  191. switch (format)
  192. { case SF_FORMAT_WAV :
  193. case SF_FORMAT_WAVEX :
  194. target = "data" ;
  195. extra = 8 ;
  196. break ;
  197. case SF_FORMAT_AIFF :
  198. target = "SSND" ;
  199. extra = 16 ;
  200. break ;
  201. default :
  202. puts ("Error : Sorry, don't handle this input file format.\n") ;
  203. exit (1) ;
  204. } ;
  205. slen = (int) strlen (target) ;
  206. lseek_or_die (fd, 0, SEEK_SET) ;
  207. printf ("Searching for '%s' maker.\n", target) ;
  208. if ((rlen = read (fd, buffer, sizeof (buffer))) < 0)
  209. { printf ("Error : failed read : %s\n", strerror (errno)) ;
  210. exit (1) ;
  211. } ;
  212. cptr = memchr (buffer, target [0], rlen - slen) ;
  213. if (cptr && memcmp (cptr, target, slen) == 0)
  214. offset = cptr - buffer ;
  215. else
  216. { printf ("Error : Could not find data offset.\n") ;
  217. exit (1) ;
  218. } ;
  219. return offset + extra ;
  220. } /* find_data_offset */
  221. static void
  222. copy_data (int fd, SNDFILE * sndfile, int readsize)
  223. { static char * buffer ;
  224. sf_count_t readlen, count ;
  225. int bufferlen, done = 0 ;
  226. bufferlen = readsize * 1024 ;
  227. buffer = malloc (bufferlen) ;
  228. while (NOT (done) && (readlen = read (fd, buffer, bufferlen)) >= 0)
  229. { if (readlen < bufferlen)
  230. { readlen -= readlen % readsize ;
  231. done = 1 ;
  232. } ;
  233. if ((count = sf_write_raw (sndfile, buffer, readlen)) != readlen)
  234. { printf ("Error : sf_write_raw returned %" PRId64 " : %s\n", count, sf_strerror (sndfile)) ;
  235. return ;
  236. } ;
  237. } ;
  238. free (buffer) ;
  239. return ;
  240. } /* copy_data */